Uploading Files Using CGI and Perl

This is an article discussion thread for discussing the SitePoint article, “Uploading Files Using CGI and Perl

I read your instructions on how to allow users to upload files to a webserver. I would like to do something like this but not post to a webserver, instead, I wanted to have a form which contains a browse button so a user can attach a picture and upon submitting the form, send their photo to an e-mail. I don’t want the picture stored anywhere just sent. I’m currently using CDONTS for my webmail - do you have any suggestions on how to do this? I’ve search Google and anything that comes close to this is either .Net (over my head) or costs $$$ in a package that I would need to buy. :frowning:

Thanks!

@cynthia : easy, you upload the file to the browser, store it, then send an email from the script and delete the pic again.

I’ve installed the script and it seems to work but for one small problem, it doesn’t place the file in the directory. Thus no photo available to show.

Okay. That looks like a good solution. Before I go away and invest several hours in figuring it out - is there a fairly simple way to do this (starting from Matt’s excellent script that works a treat)?

Steve

Does this script allows for large files, files greater than 2MB?

Hi.

Yes, it seems to. For my application it probably needs to.

S

Hi, How about uploading file2.pl on file1.pl
should the user click on button “report”
my email is alifouad@ambros.plus.com

many thanks

You should use read() instead of <> when copying the file so your script doesn’t crash if you have a single line thats 100 MB long.

I am getting an error! I think my absolute path for $upload_dir is wrong. I put #!/usr/bin/perl -w
$upload_dir = “/upload”;
Because I just created a folder called upload in the root. Is this right?? email me at vonoven1@cox.net if you can help me.

Is this safe for me to use on a shared server or will my ISP flip out…?

Sorry for my ignorance, but is it possible for me to use this script on a shared server, or will my ISP freak out about security?

Thanks

Ron

Hi Ron,

I’m the author of this article. :slight_smile: You should be OK running this script on a shared server, but if you’re concerned, then talk to your ISP first before using it.

Matt

Brad,

I am getting an error! I think my absolute path for $upload_dir is wrong. I put #!/usr/bin/perl -w
$upload_dir = “/upload”;
Because I just created a folder called upload in the root. Is this right??

Yes, it looks like your $upload_dir is probably incorrect. It should include the absolute path to your web site’s document root folder, followed by “/upload”. For example, it might be something like:

$upload_dir = "/home/sites/abc123/public_html/upload";

If you’re not sure of your web site’s absolute path, ask your hosting provider.

Hope that helps! :slight_smile:
Matt

Hi, I wrote your script to upload files but it does not seem to work correctly and I’ve done everything correctly. Can you please check it out and notify if you see any mistakes? Thanks! Joe

#!/usr/bin/perl -w
use CGI;

$upload_dir = “http://www.jlreyes.com/cgi-bin/upload”;

$query = new CGI;

$filename = $query->param(“photo”);
$email_address = $query->param(“email_address”);
$filename =~ s/.*\/\\/$1/;
$upload_filehandle = $query->upload(“photo”);

open UPLOADFILE, “>$upload_dir/$filename”;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;

print $query->header ( );
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your photo!</P>
<P>Your email address: $email_address</P>
<P>Your photo:</P>
<img src=“http://www.jlreyes.com/cgi-bin/upload/$filename” border=“0”>

</BODY>
</HTML>

The script is located at: http://www.jlreyes.com/temp/upload2004.html

my email is: jreyes@aboutreal.com

When I run this I get the following error: Software error:
Can’t locate object method “new” via package “CGI” at upload.cgi line 8.

I don’t understand what this means. Any help? I’m obviously brand new at this.

#!/usr/bin/perl -s

use CGI::Carp qw/fatalsToBrowser/;

$upload_dir=“/home/wmdband/www/upload”;

$query = new CGI;

$filename = $query->param(“photo”);

$filename =~ s/.*\/\\/$1/;

$upload_filehandle = $query->upload(“photo”);

open UPLOADFILE, “>$upload_dir/$filename”;

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;

Hi Daniel,

It sounds like the machine you’re running the Perl script on doesn’t have the CGI.pm library installed:

http://stein.cshl.org/WWW/software/CGI/

Also make sure you have “use CGI;” at the top of your script.

Matt

Hello,

I copy-pasted the code exactly as per your instructions (after changing the paths), but I seem to be facing a problem with the upload. The uploaded file is getting created alright (with the correct filename) however its file size is always zero. Could you please help?

Sounds like you have some kind of file/directory permission problem. Maybe your hosting company doesn’t allow your scripts to write to /tmp?

Matt

Hi,

I’m getting this error using Matt’s upload.cgi and file_upload.html

Not Found
The requested URL /upload/upload.cgi was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.31 Server at gardening101.org Port 80

Has anyone run into this problem and is there a solution?