LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PERL Upload Files (https://www.linuxquestions.org/questions/programming-9/perl-upload-files-394362/)

dsheller 12-18-2005 11:29 PM

PERL Upload Files
 
Code:

#!/usr/bin/perl

use CGI;

$upload_dir = "/pop_script/";

$query = CGI->new();

print $query->header();
print "File is uploading, please wait...<br>\n";

$filename = $query->param("upload");
$filename =~ s/.*[\/\\](.*)/$1/;

$outputfile = $upload_dir . $filename;
open UPLOADFILE, ">$outputfile";

while ( <$filename> ) {
        print $_;
    print UPLOADFILE $_;
}

close UPLOADFILE;

print "Finished uploading file (".$filename.")!\n";

That is the PERL code I am trying to use to get the files uploaded...
Now the form is standard form with a file input and i'm positive the name value is equal to "upload"

Also I have chmodded the directory to 777 so it should be able to write even as user apache. The problem is this: no matter what file is sent to it it always writes 0 bytes. I sent a 50 MB file and it took a while, which implies it was uploading, as well as a simple text document. Both wrote 0 bytes. Anyone know whats going on?

EDIT: Evidently FireFox works fine to upload files, but IE fails. Any idea why IE would fail on this?

bigearsbilly 12-19-2005 04:27 AM

I think you need to use the read call.
Try this:

Code:

#!/usr/bin/perl

use CGI;

$upload_dir = "/pop_script/";

$query = CGI->new();

print $query->header();
print "File is uploading, please wait...<br>\n";

$filename = $query->param("upload");
$filename =~ s/.*[\/\\](.*)/$1/;

$outputfile = $upload_dir . $filename;
open UPLOADFILE, ">$outputfile";

my $buffer;
while ( read($filename, $buffer, 1024)) {
        print $buffer;
        print UPLOADFILE $buffer;
}

close UPLOADFILE;

print "Finished uploading file (".$filename.")!\n";


dsheller 12-19-2005 03:13 PM

Well I tried what you have there... but it still doesn't work in IE. Works perfectly in FF though!

spoody_goon 12-19-2005 04:17 PM

does your form html tag include enctype="multipart/form-data" like this?
<form name="form" method="post" enctype="multipart/form-data" action="">

I don't know what the browser would have to do with the cgi script though that has me curious.

dsheller 12-19-2005 06:18 PM

Code:

<FORM ACTION="/cgi-bin/upload.pl" METHOD="post" ENCTYPE="multipart/form-data">
 <center>
 <table cellpadding="5" border="1">
 <tr>
 <td bgcolor="#A49FA4">File to Upload: <INPUT TYPE="file" NAME="upload"></td>
 </tr>
 <tr>
 <td bgcolor="#A49FA4" align="center"><INPUT TYPE="submit" NAME="Submit" VALUE="Upload"></td>
 </tr>
 </table>
 </center>
 </FORM>

So... yes. IT does.

keefaz 12-19-2005 06:34 PM

try add a : print "$filename";
right before the reg exp to see how IE and FF see the filename

dsheller 12-19-2005 06:43 PM

Internet Explorer:
Before REGEX: C:\Documents and Settings\David\Desktop\test.txt
After REGEX: test.txt

FireFox:
Before REGEX: test.txt
After REGEX: test.txt

spoody_goon 12-19-2005 09:47 PM

It has been a while since I worked with perl but I think you need a serparate varible for the filehandle. because you clean up filename and then try to us it as a handle
Code:

#!/usr/bin/perl

use CGI;

$upload_dir = "/pop_script/";

$query = CGI->new();

print $query->header();
print "File is uploading, please wait...<br>\n";

$filename = $query->param("upload");
$filename =~ s/.*[\/\\](.*)/$1/;
$filehandle = $query->param("upload");

$outputfile = $upload_dir . $filename;
open UPLOADFILE, ">$outputfile";
binmode UPLOADFILE;
while ( <$filehandle> ) {
        print $_;
    print UPLOADFILE $_;
}

close UPLOADFILE;

print "Finished uploading file (".$filename.")!\n";


dsheller 12-20-2005 12:12 AM

No go. It still is giving 0 bytes in IE.

keefaz 12-20-2005 04:50 AM

Try the script posted by spoody_goon, but rather of
use $query->param() to get the file handle, use
the CGI method $query->upload() :

Code:

$filehandle = $query->upload("upload");

bigearsbilly 12-20-2005 05:50 AM

Remember, while doing CGI
tail -f /var/apache/logs/error_log


It's this:

Code:

$filename =~ s/.*[\/\\](.*)/$1/;
you are changing the slashes before uploading so IE will not see it as a filename.

firefox is clever enough to convert back for you.
save the name first.
This works for me:
Code:

#!/usr/bin/perl

use CGI;

$upload_dir = "/pop_script/";

$query = CGI->new();

print $query->header();
print "File is uploading, please wait...<br>\n";

$filename = $query->param("upload");

$outputfile = $filename ;
$outputfile =~ s/.*[\/\\](.*)/$1/;
$outputfile = $upload_dir . $outputfile ;


open UPLOADFILE, ">$outputfile" or die "$outputfile: $!";
my $buffer;
while ( read($filename, $buffer, 1024)) {
        print qq(<pre>$buffer</pre>);
        print UPLOADFILE $buffer;
}
close UPLOADFILE;

print "Finished uploading file (".$filename.")!\n";


dsheller 12-22-2005 11:29 PM

Hey thanks bigbears, that works great!


All times are GMT -5. The time now is 02:20 AM.