469,271 Members | 1,000 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

CGI upload creates empty file on server?

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
Jul 19 '05 #1
1 5899
kr******@detandetfirma.dk (Kristian) wrote in message news:<c9**************************@posting.google. com>...
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.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dave | last post: by
5 posts views Thread by Ariel Dolan | last post: by
reply views Thread by Buddy Home | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.