homepage Welcome to WebmasterWorld Guest from 54.161.220.160
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Perl File Upload Error - No such file or directory
Perl Error
Rockchick



 
Msg#: 4168012 posted 8:11 am on Jul 11, 2010 (gmt 0)

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"
enctype="multipart/form-data">
<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>
</form>


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).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
print <<END_HTML;
<html>
<head>
<title>Thanks!</title>
<style type="text/css">
img {border: none;}
</style>
</head>
<body>
<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>
</body>
</html>
END_HTML
;


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 :)
Rockchick

 

phranque

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



 
Msg#: 4168012 posted 8:34 am on Jul 11, 2010 (gmt 0)

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.

Rockchick



 
Msg#: 4168012 posted 8:42 am on Jul 11, 2010 (gmt 0)

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 :)

phranque

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



 
Msg#: 4168012 posted 11:03 am on Jul 11, 2010 (gmt 0)

i would suggest:
my $filename = $query->param("file_upload");
if ( !$filename ){
print $query->header( );
print $query->cgi_error();
print "There was a problem uploading your photo (try a smaller file).";
exit;
}

Rockchick



 
Msg#: 4168012 posted 8:19 pm on Jul 11, 2010 (gmt 0)

Thanks for the suggestion phranque, I tried it but comes up with this error:

Global symbol "$email_address" requires explicit package name at test.cgi line 38.
Execution of test.cgi aborted due to compilation errors.


I think all variables need a package name.

Hmmm this isn't going to be as easy as I thought.... any idea's?

Thank you :)

chorny

5+ Year Member



 
Msg#: 4168012 posted 11:05 pm on Jul 11, 2010 (gmt 0)


I think all variables need a package name.

They need to be declared with "my".


Hmmm this isn't going to be as easy as I thought.... any ideas?

Please repost your final code with all changes.

phranque

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



 
Msg#: 4168012 posted 11:18 pm on Jul 11, 2010 (gmt 0)

do you still have this line of code in your script?
my $email_address = $query->param("email_address");

Rockchick



 
Msg#: 4168012 posted 11:26 pm on Jul 11, 2010 (gmt 0)

Thanks chorny :)

They need to be declared with "my".

Yep I have done that... but when I took this line out:
my $email_address = $query->param("email_address");
I got the error in my post above. So have put it back in.

So... at the moment it stands as this:
on my html doc:
<form action="/cgi-bin/test.cgi" method="post"
enctype="multipart/form-data">
<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>
</form>


and the test.cgi file:

#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "http://www.example.com/uploads";
my $query = new CGI;
my $filename = $query->param("file_upload");
my $email_address = $query->param("email_address");
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->redirect ( "http://www.example.co.nz/success.html" );


And finally, I'm still getting the error:
There was a problem uploading your photo (try a smaller file).


There's a problem uploading the file somewhere... I have set the permissions on the .cgi file at 755 and the uploads folder to 777. So they are all good.

[edited by: phranque at 3:02 am (utc) on Jul 12, 2010]
[edit reason] exemplified domains [/edit]

Rockchick



 
Msg#: 4168012 posted 11:33 pm on Jul 11, 2010 (gmt 0)

phranque:
do you still have this line of code in your script?
my $email_address = $query->param("email_address");


When I took that line out phranque, I got the error:
Global symbol "$email_address" requires explicit package name at test.cgi line 38.
Execution of test.cgi aborted due to compilation errors.


so... for now, have put it back in.

Rockchick



 
Msg#: 4168012 posted 12:34 am on Jul 12, 2010 (gmt 0)

Ok I got it working! The $upload_dir was wrong.... argh! I knew it would be something staring me in the face!

Thank you all for your help :)

One last question, if you don't mind... you know how I have email address on the form? Well.... where does that parameter go? I mean, I will get the file that has been uploaded by the person, but where will I find their email address?

phranque

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



 
Msg#: 4168012 posted 2:25 am on Jul 12, 2010 (gmt 0)

the value entered by the visitor in the "email_address" form field is the value returned by $query->param("email_address").
is that what you are asking?

Rockchick



 
Msg#: 4168012 posted 6:55 am on Jul 12, 2010 (gmt 0)

I got it all up and working - so when I browse to a file, enter my email and hit submit - it all works great. Then I check on my server and the file is there. But I'm wondering where I find the email that I submitted...

(for example if my client uploads a file, and enters their email - it would be helpful if I can then see that and identify who sent what... rather than just have all these uploads in there and not knowing what belongs to who)

phranque

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



 
Msg#: 4168012 posted 7:26 am on Jul 12, 2010 (gmt 0)

i think i misunderstood your question about the where the email address was.

2 options for associating the email address to the uploaded file:

- you can use some type of database to store the relationship between the email and the file; this could even be implemented with a flat file solution.

- you could use the email address in your file naming convention. (for example, such as mailbox@example.com_filename.jpg)
note that you can rename the image file without the email address before serving it if necessary.

Rockchick



 
Msg#: 4168012 posted 8:45 am on Jul 12, 2010 (gmt 0)

Oookay sounds like it's way above my head :(
That's ok, I will do some research on it... Google is my best friend! haha
Thanks for all your help :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved