CGI upload creates empty file on server?

K

Kristian

The script below uploads the selected file and creates it on the
server in the right directory and all looks fine. But the file is
empty, it contains nothing...

HTML FORM:
<form name="foo" method="post" action="../upload_image.pl"
target="_self" enctype="multipart/form-data">
<input type="file" name="file"><input type="text"
name="filename"></form>

Perl script:

#!/usr/bin/perl

use CGI;
use Cwd;

#print "Content-type: text/html \n\n";

#Setup query from cgi
my $cgi = new CGI;

#Setup upload directory
$uploaddir = "/client_image_uploaddir";
$file = $cgi -> param("file");
$file =~ s/.*[\/\\](.*)/$1/;
$filename = $cgi -> param("filename");
$datestr = time;

#Handle the upload
open FHUP, ">$uploaddir/$file" or die;
binmode FHUP;

undef $Buffer;
while( read( $file, $Buffer, 1024 ) ){
print FHUP $Buffer;
}
close (FHUP);

print $cgi -> header();

print "<h3>Filen '$filename' blev uploaded!</h3>";
print "<img src=client_image_uploaddir/$file alt=$filename> ";
print "<a href=\"javascript:void(history.back());\">Tilbage</a> <a
href=\"javascript:void(window.close());\">Luk vindue</a>";

I also tried without 'buffering' and the result is the same, file is
created but with no content.

Any ideas?

/Kristian
 
N

nobull

The script below uploads the selected file and creates it on the
server in the right directory and all looks fine. But the file is
empty, it contains nothing...


#!/usr/bin/perl

If you had used strict and warnings you would have got errors that
would have given you a clue as to what was wrong with your program.

Why do people stubbonly refuse to help themselves?
$file = $cgi -> param("file");
$file =~ s/.*[\/\\](.*)/$1/;

The thing returned by param() for an upload field is magically both a
string and a file handle.

IMNSHO this was poor design in CGI. Consider using the upload method
for upload fields.

Anyhow, once you start to mess with the magic thing as a string, the
magic is lost and it becomes an ordinary string.

If you then try to use a the string as a filehandle then you'd usually
get an error. That's because _usually_ Perl5 scripts have "use
strict" at the top. If you leave out the "use strict" then (for
reasons of Perl4 compatability) the string is interpreted as a
symbolic reference to a file handle.

I suggest you find the person who advised you not to put "use strict"
at the top of your scripts, shake them warmly by the throat, and
explain how not doing so has wasted your time.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top