cranberry13 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I've been spending hours trying to figure out the problem with my upload file program. I have a form where a user presses the 'browse' button , chooses a file on their local directory and then my cgi script downloads it and prints it to a directory. I created this so that people can upload their images.

The form looks like this:
<form action="profile_step4.pl" enctype="multipart/form-data" > choose file <input type="file" name="photo1"> <input type="submit"> </FORM>
Then, in my "profile_step4.pl" script, I have the following:

#!/usr/local/bin/perl use CGI; print "Content-type: text/html\n\n"; &GetAllCookies; my $userid = $Cookies{userid}; $upload_dir = "/usr/home/tiri/htdocs/upload/"; ## not the real path . +.. but the path is to my htdocs/upload directory (which already exist +s) $mydir .= $upload_dir .$userid ; `mkdir $mydir`; ## create the directory for this user print "created the dir $mydir"; $query = new CGI; $filename = $query->param("photo1"); $upload_filehandle = $query->upload("photo1"); $filename=~ s/.*[\/\\](.*)/$1/; open (UPLOADFILE, ">$mydir/$filename")|| die $! ; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;

Any suggestions on why this isn't working? When i run the program, i was able to determine that the program dies on the statement:

$upload_filehandle = $query->upload("photo1");
Why is it dying here? Do I need to set the 'upload' directory with any special permissions?

Replies are listed 'Best First'.
Re: Problems uploading file
by Grygonos (Chaplain) on Jun 30, 2004 at 14:06 UTC

    I wrote a script that did something similar in college, but couldn't tell you the exact problem offhand. You have the basic premise right. I would look at CGI::Carp and then you can truly tell where it bombs. Also drop use strict and use warnings in the top of your script.. will help keep you from making simple typographical errors, and warn you of potential misbehavior

    Also, yes if you are running apache, the upload directory needs to be writable by the apache userid. I setup a usergroup for apache and the host user (where the page was located... say /home/grygonos/public_html/ and marked it 770 or 775 .. like I said its been a few years, but there are permission issues to be aware of yes.

Re: Problems uploading file
by ercparker (Hermit) on Jun 30, 2004 at 15:58 UTC
    I agree with Grygonos
    your code syntax appears correct. I would use
    CGI::Carp qw(fatalsToBrowser);
    and create a directory with the corrent permissions to test writing a file to.
Re: Problems uploading file
by iburrell (Chaplain) on Jun 30, 2004 at 19:01 UTC
    What does the error log say when it dies? We can't help you otherwise.

    One thing I see is that you don't have a method parameter on the form. GET is the default so you must have method="POST" on the form element.

    Another problem could be a wrong name. Check if the filename is undef because expected parameter was not passed. Another possible problem is that the upload failed or could not be parsed. This would definitely happen when using GEt instead of POST. The upload method should return undef, and the error will be in cgi_error. Check the error condition.