CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Script to dynamically generate links to the files present in current directory

 



terrykhatri531
User

Jul 26, 2014, 12:06 AM

Post #1 of 11 (910 views)
Script to dynamically generate links to the files present in current directory Can't Post

Hi,

I am trying to create dynamic links for the contents present in the given directory for downloading, the following is a php script that needs to converted to perl, please help !!




Code
 
$dir_open = opendir('http://192.168.56.105/reports/');

while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$link = "<a href='./$filename'> $filename </a><br />";
echo $link;
}
}

closedir($dir_open);


Many many thanks

Terry


terrykhatri531
User

Jul 26, 2014, 1:20 AM

Post #2 of 11 (905 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Hi,

Ok I have converted it to perl as under :


Code
 
my $dir = 'http://192.168.56.106/reports';
my $dir_open = opendir(DIR,$dir) or die $!;
my $link;

while (my $file = readdir(DIR)) {
$link = "<a href='$dir/$file'> $file </a><br />";
print $link;
}
closedir(DIR);
exit 0;


But it fails to open the $dir, my DocumentRoot is /var/www/html the reports folder is located under DocumentRoot i.e. /var/www/html/reports.

Any idea why it does not work !!

Many thanks

Terry


Laurent_R
Veteran / Moderator

Jul 26, 2014, 4:13 AM

Post #3 of 11 (899 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Your code would probably work on a local disk and directory, but here, you need to first to connect to a distant platform via the network. You might want to use a module such as Net::Telnet, Net::SSH or some other, depending on the type of network you have.


FishMonger
Veteran / Moderator

Jul 26, 2014, 6:28 AM

Post #4 of 11 (892 views)
Re: [Laurent_R] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

I don't think Terry is trying to connect to a remote system. I think the use of http:// is a "red herring".

Terry, try changing:

Code
my $dir = 'http://192.168.56.106/reports';

To:

Code
my $dir = '/var/www/html/reports';



terrykhatri531
User

Jul 26, 2014, 7:45 AM

Post #5 of 11 (887 views)
Re: [FishMonger] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Thanks Ron,



Another question, from the links created I am trying to execute another script passing the $file variable like :




Code
 
while (my $file = readdir(DIR)) {
if($file != "." && $file != ".."){
$link = "<a href='viewreports2.pl?go=Details&sfold=$file'> $file <img src='images/grey.png'></a><br />";
print $link;
}
}


The variable value gets passed as desired however the other script i.e. viewreports2.pl does not create the links in sfold i.e. subfolder, the url with the $file passed is :

http://192.168.56.105/perlproj/cgi-bin/viewreports2.pl?go=Details&sfold=2014-07-26



Here is the code from other script :


Code
 
my $q = new CGI;
my $session = CGI::Session->load();
my $sfold = $q->param('sfold');

........

my $dir = "reports/$sfold";
my $dir_open = opendir(DIR,$dir) or die $!;
my $link;

while (my $file = readdir(DIR)) {
if($file != "." && $file != ".."){
$link = "<a href='$dir/$file'> $file <img src='images/grey.png'></a><br />";
print $link;
}
}
closedir(DIR);


I have no clue what I am doing wrong here, I am not getting any errors and DO NOT see anything in the apache logs either, is this the problem because there are hyphens in the $file value like 2014-07-26 ?



Please DO HELP !!!

Many thanks.

Terry


FishMonger
Veteran / Moderator

Jul 26, 2014, 8:24 AM

Post #6 of 11 (880 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

What do you mean by

Quote
viewreports2.pl does not create the links in sfold i.e. subfolder


Is $file in the first script a directory name or a file name?


terrykhatri531
User

Jul 26, 2014, 8:59 AM

Post #7 of 11 (876 views)
Re: [FishMonger] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Hi Ron,

Its a direcotory but I don't think there should be problem for the other script to receive the variable value into $sfold and create the links for the contents of subfolder ?

Terry


FishMonger
Veteran / Moderator

Jul 26, 2014, 9:15 AM

Post #8 of 11 (872 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

What is the exact link that viewreports2.pl generates and what happens when you click it?

What happens if you manually enter that url in the browser?

What kind of files do the links point to?

Do you have error handling in both of those scripts so that you can catch any/all errors and warnings i.e., do you have this in the scripts?

Code
use strict; 
use warnings FATAL => qw(all);
use CGI::Carp qw(fatalsToBrowser);



terrykhatri531
User

Jul 26, 2014, 9:28 AM

Post #9 of 11 (869 views)
Re: [FishMonger] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post


In Reply To
What is the exact link that viewreports2.pl generates and what happens when you click it? Links of directories in the subdirectory of 2014-07-24 directory

What happens if you manually enter that url in the browser? No links

What kind of files do the links point to? Directories

Do you have error handling in both of those scripts so that you can catch any/all errors and warnings i.e., do you have this in the scripts?

Code
use warnings FATAL => qw(all);   
I go the error this time i.e. Argument "." isn't numeric in numeric ne (!=) at /usr/share/perlproj/cgi-bin/viewreports2.pl line 55.










And ne works !!!

Thanks Ron for your smart help.

Rgds.

Terry


(This post was edited by terrykhatri531 on Jul 26, 2014, 9:33 AM)


terrykhatri531
User

Jul 26, 2014, 11:51 AM

Post #10 of 11 (858 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Hi Ron/Chris,

I have another issue if I click on the following link on the local host where application is hosted the file gets downloaded immediately however from remote host it does not get downloaded at all it takes forever and finally times out, any idea why ?



Link :


Code
 
$link = "<a href='$dir/$file' target='_blank' > $file <img src='images/file.png'></a><br />";




Permissions for the Reports directory in httpd.conf


Code
 
<Directory /usr/share/perlproj/cgi-bin/reports/>
Options Indexes MultiViews FollowSymLinks
Order allow,deny
Allow from all
Allow from 192.168.56.10
</Directory>


In the apache access.log I see the following :


Code
 
192.168.56.10 - - [26/Jul/2014:23:26:13 +0500] "-" 408 - "-" "-"




And nothing in the error log.



Though its not perl related but since we are on the subject and I am sure you gurus from your experience know as to why it works from localhost but not from remote host or can help me figure out why.



Many many thanks !!!

Terry


Zhris
Enthusiast

Jul 26, 2014, 3:07 PM

Post #11 of 11 (850 views)
Re: [terrykhatri531] Script to dynamically generate links to the files present in current directory [In reply to] Can't Post

Hi Terry,

I haven't been following this thread, but on a sidenote:

Make sure you validate your 'sfold' parameter. Your users could supply current / upper working directory segments and access forbidden directories. To restrict access to parent directories cleanly while still allowing use of these segments, Cwd's abs_path method can be used to resolve the segments, then you can compare the resolved path against a fixed base path.

It is also best to use File::Spec to filter out the current / upper working directory segments via the no_upwards method, when processing directory contents:


Code
use File::Spec::Functions; 

my @files = no_upwards(readdir(DIR));


Chris


(This post was edited by Zhris on Jul 26, 2014, 3:27 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives