Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

download files

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I experience some difficulties to perform a script to be able to download some files via HTTP.

The Perl script is located on a Linux in a server ("/var/ where I can run it via Internet Explorer.
I'm able to display the directory content ("/home/toto") but I'm not able to open or save the files from this directory to my local machine (Windows XP by example).

My goal is to be able to save a file when I click on them and choose the target on my local machine.

Can someone help me to fix this script? thanks in advance.

==========
#!/usr/bin/perl
use CGI;

$|=1;

my $query=new CGI;

print qq~
<table align="center" width="90%">
<tr><td><font color="black" face="Arial" size="4"><b>Files in current directory</b></td></tr>
<tr>
<td valign="top">
<font color="black" face="Arial" size="2">
~;
$dir="/home/toto";
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);


foreach(@current){
unless($_ eq '.' || $_ eq '..' || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~


<a href="$dir/$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="$dir/$currentfiles[$aa]"> </a></font><br>
~;
}
print qq~
</font></tr>
</table>
 
Hi Kevin,

I agree with you that I need a different script and I tried that but it did not work. Here is the part of the script download1.pl that I tried to change but it did ot work and I think that the conditional test "if else" does not work with HREF :

==============
for($aa = 0; $aa <= $#currentfiles; $aa++){
print qq~

if( -f $currentfiles[$aa]) {

<a href="download.pl?file=$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="cgi-bin/download.pl?file=$currentfiles[$aa]"> </a></font><br>

}

if( -d $currentfiles[$aa]) {
<a href="open_dir.pl?file=$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a><a href="cgi-bin/open_dir.pl?file=$currentfiles[$aa]"> </a></font><br>
}
==============

open_dir.pl is a script to list the subdirectory content.

What is it wrong with my script here?

Thanks
 
Code:
foreach my $file (@currentfiles){
   if( -f "/full/path/to/$file") {
      print qq~ <a href="download.pl?file=$file">$file</a><br>
~;
   }
   elsif( -d "full/path/to/$file") {
      print qq~<a href="open_dir.pl?file=$$file">$file</a><br>
~;
   }
}

if $file already has the full path in it you don't need to add the path, or if the script is in the same directory where the files are you can just use $file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
where you see $$file above it should be $file

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

Thanks a lot for your great suggestions and it works fine.

The script open_dir.pl should do the same work as download1.pl, I tried to use download1.pl content in open_dir but it does not work when I try to list the subdirectory content. I think that I have to use something like a recursive script. Have you any suggestion for that?

Here is download1.pl:

==================
#!/usr/bin/perl
use CGI;

my $query=new CGI;
print $query->header;
print $query->start_html(
-bgcolor=>"#ffffff",
-link=>"blue",
-vlink=>"#00ffff",
-alink=>"red",
-text=>"#000000");

print qq~
<table align="center" width="90%">
<tr><td><font color="black" face="Arial" size="4"><b>Files in current directory</b></td></tr>
<tr>
<td valign="top">
<font color="black" face="Arial" size="2">
~;
$dir=".";
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);

foreach(@current){
unless($_ eq '.' || $_ eq '..' || $_ =~ m/^\./ || $_ =~ m/^download*/){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;
foreach my $file (@currentfiles){
if( -f "$file") {
print qq~ <a href="download.pl?file=$file">$file</a><br>
~;
}
elsif( -d "$file") {
print qq~<a href="open_dir.pl?file=$file">$file/</a><br>
~;
}
}

print qq~
</font></tr>
</table>
</html>~;
==================


Thanks.
 
The problem is that download1.pl is not importing the paramater you send it to use to open the directory.

file=$file <-- $file should be the directory

instead of:

$dir = '.';

you need something like:

my $dir = $query->param('file');

But anytime you are using user input to do things like opening files you really need to filter and validate the input before using it in the script. Judging by the code you have been posting I don't think you are that far along in your perl progress. What is this script being used for?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

I found a way to be able to access to the subdirectories and return back to the first directory. Not easy for me because I learn to use this kind of programming.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top