Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl File Upload Error - No such file or directory

Perl Error



8:11 am on Jul 11, 2010 (gmt 0)

5+ Year Member

Hi guys,

My first time here in a web forum, and you all seem like a helpful bunch. Always good to find a friendly place where I can lend a hand or get a helping hand :)

Hoping someone can help.... when I hit submit on my link, the error says:
No such file or directory at test.cgi line 31

Here is line 31 of test.cgi:
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";

Now, here is the html code:
 <form action="/cgi-bin/test.cgi" method="post" 
<p>File: <input type="file" name="file_upload" /></p>
<p>Email Address: <input type="text" name="email_address" /></p>
<p><input type="submit" name="Submit" value="Submit Form" /></p>

And here is the code from the test.cgi file:
#!/usr/bin/perl -wT 
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
$CGI::POST_MAX = 1024 * 5000000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/uploads";
my $query = new CGI;
my $filename = $query->param("photo");
my $email_address = $query->param("email_address");
if ( !$filename )
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
$filename = $1;
die "Filename contains invalid characters";
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
while ( <$upload_filehandle> )
print $query->header ( );
print <<END_HTML;
<style type="text/css">
img {border: none;}
<p>Thanks for uploading your photo!</p>
<p>Your email address: $email_address</p>
<p>Your photo:</p>
<p><img src="/upload/$filename" alt="Photo" /></p>

Once I get this working, I will be adapting it so that my clients can upload any kind of file to me (it's not going to be just photo's).

I hope someone can help, this has driven me nuts and please be gentle - I'm still rather new at all this! lol

Cheers guys,
From way down under in NEW ZEALAND :)


8:34 am on Jul 11, 2010 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome to WebmasterWorld [webmasterworld.com], Rockchick!

i see a couple of possible problems:
my $upload_dir = "/uploads";

the open is going to look for the "uploads" directory at the root of the filesystem, not the document root directory.

my $filename = $query->param("photo");

i don't see a parameter named "photo" in your form.


8:42 am on Jul 11, 2010 (gmt 0)

5+ Year Member

Thanks Phranque for the warm welcome! :)

Oookay... well yep, those fixed that problem! Thank you! I knew it would be something simple that I had just not seen!

But... one more problem... now when I got try the script, I Browse to a pic (and it's only 22KB, but I get the error:
There was a problem uploading your photo (try a smaller file).

Do you reckon it's ok to take this line out:
$CGI::POST_MAX = 1024 * 5000000; 

Thanks again :)

Software error:

Can't locate /home/deploy/webmasterworld/code_format-v6.lib in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at decode-post-v6.lib line 27, <THREADDAT> line 5.

For help, please send mail to the webmaster (it@imninjas.com), giving this error message and the time and date of the error.