PDA

View Full Version : need cgi upload help


masterofollies
05-24-2005, 11:37 PM
cgi is enabled on my hosting provider but can anyone help me with this...
I wish to have a text field and a upload button on my website so that a person can upload an image file to my server so I can put it on the website. I need it to go into the basic cgi-bin or whatever it stores uploads in. Can anyone help me make this function?

mlseim
05-25-2005, 04:09 AM
If your host allows PHP, use the code below ... very simple and no
Perl experience needed with uploading and CHMODding.

This is the HTML form that allows up to 3 files to be uploaded:
<form action='upload.php' method='post' enctype='multipart/form-data'>
<input type='hidden' name='num' value='3'>
Select File: <input type='file' name='file1'><br>
Select File: <input type='file' name='file2'><br>
Select File: <input type='file' name='file3'><br>
<input type='Submit' value='Upload'>
</form>

Create a subdirectory called "uploads" where your files will be saved.

This is the PHP script. Save in your regular HTML directory (the same
directory as your HTML form. Save as the filename: upload.php
<?php
//Get the number of files to be uploaded
$num = $_POST['num'];
//Start the Loop
for ($x = 1; $x <= $num; $x++)
{
//Make sure a file has been selected for upload
if (is_uploaded_file($_FILES['file'.$x]['tmp_name']))
{
//Get the size of the current file
$size = $_FILES['file'.$x]['size'];
//Make sure that this file is <= to 500KB (or whatever size limit you want).
if ($size <= 500000)
{
//Move the uploaded file ... "uploads/" is where they will be stored.
if (move_uploaded_file($_FILES['file'.$x]['tmp_name'],'uploads/'.$_FILES['file'.$x]['name']))
{
//Display message
echo "File: ".$_FILES['file'.$x]['name'] . " uploaded.<BR>";
}
else
{
//Unable to move file
echo "File: ".$_FILES['file'.$x]['name']. " error moving file.<BR>";
}
}
else
{
//File too large
echo "File: " . $_FILES['file'.$x]['name'] . "File Too Large.<BR>";
}
}
else
{
//No file selected
echo "No File Selected.<BR>";
}
} //End of loop
?>

masterofollies
05-25-2005, 04:35 AM
Still has a problem. I put both codes into one file and run it and then tried to put the html code into a blank html page and the php code in a php file. Here is what it said when I tried to make one upload.

Warning: move_uploaded_file(uploads/armor2.jpg): failed to open stream: Permission denied in /home/creative/public_html/upload.php on line 16

Warning: move_uploaded_file(): Unable to move '/var/tmp/phproB7QM' to 'uploads/armor2.jpg' in /home/creative/public_html/upload.php on line 16
File: armor2.jpg error moving file.
No File Selected.
No File Selected.

nkrgupta
05-25-2005, 07:15 AM
well..this is the code in Perl to upload a single file to the server and retrieve it to view in your site--

In your HTML form , write this--

<FORM ACTION="/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">

Photo to Upload: <INPUT TYPE="file" NAME="photo">

<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
#Modify the path to the upload.cgi according to ur needs.

Now for the upload.cgi, i.e. ur perl script--

$upload_dir = "/home/mywebsite/htdocs/upload"; ##make this upload dir outside ur cgi-bin
$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;

And ur done with uploading the picture to the server. As for the retrieval, just use

<img src="http://yoursite.com/upload/$filename" border="0">
anywhere in the page where u want to show the picture

All the best
Naveen

mlseim
05-25-2005, 01:04 PM
After you created a directory called "uploads", your webhost may require
you to give permission to save into that directory. You'll have to try to
CHMOD the directory to 777 (using your FTP program). Or, possibly your
webhost has an online page (your site admin page) that lets you set
write permissions on a directory.

Using the Perl version, you'll probably have to do that also.

masterofollies
05-26-2005, 07:47 PM
I set the attibutes to 777 and it gives me an 500 error. I looked it up and it says its a cgi/perl error. The web host provides it but the code isn't working. Here is the html file of it,

<FORM ACTION="/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">

Photo to Upload: <INPUT TYPE="file" NAME="photo">

<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">

And here is the cgi part.

$upload_dir = "/uploads"; ##make this upload dir outside ur cgi-bin
$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;

mlseim
05-27-2005, 01:29 AM
You realize of course that "nkrgupta" only gave you a portion
of the Perl script. There needs to be a shebang (the first line),
and some other stuff. Ask him to post the whole thing.

I use the PHP version that I posted all the time. I've never had
a problem with it. I still think it's a directory permission problem.

nkrgupta
05-27-2005, 05:57 AM
well...in that case...here's the complete thing u'd require to upload a picture to your website.

The HTML --

<HTML>
<HEAD></HEAD>
<BODY>
<FORM ACTION="upload.cgi" METHOD="post" ENCTYPE="multipart/form-data"> (If your html page is not in the same page as the cgi script, then u need to give the full absolute path of the script instead of just upload.cgi)
Photo to Upload: <INPUT TYPE="file" NAME="photo">
<br><br>
Your Email Address: <INPUT TYPE="text" NAME="email_address">
<br><br>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
</FORM>
</BODY>
</HTML>

The above page would display only a file field to select the picture and a text field for entering the email of the user.You can add more fields like name, address etc. according to your needs.

And here's the complete cgi script-

#!/usr/bin/perl -w

use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

$upload_dir = "/uploads";

my $query = new CGI;
print $query->header();
warningsToBrowser(1);
print $query->start_html();

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

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

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

close UPLOADFILE;

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

<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>
<BODY>
<P>Thanks 4 uploading your photo!</P>
<P>Your email address: $email_address</P>
<P>Your photo:</P>
<img src="/uploads/$filename" height="200" width="200">
</BODY>

END_HTML

print $query->end_html();

You must check twice that the permissions for the upload directory and the upload.cgi script are appropriately set, and path to show the picture corresponds to the $upload_dir path.And you can check to see manually whether the picture has been uploaded to the server or not by using an ftp program or any other means your webhost has provided.
If all's fine then u must get this thing going.
Naveen

FishMonger
05-27-2005, 06:55 AM
A couple comments/suggestions reguarding Naveen's script.

1) The header is being printed 3 times (twice using $query->header() and once hard coding the tags) and the body tags are printed twice (once hard coded and once using $query->start_html(), $query->end_html()). These should be printed only once.

2) It should have error handling on the opening of the filehandle and on retrieving the $query->param("????").

3) The success/failure of the upload should be verified so you can print the proper closing message.

Naveen and I could help resolve these items or you can work with it as is.

nkrgupta
05-27-2005, 10:35 AM
huh....how could i miss the headers being printed THRICE!!!! guess i need to proof read more before posting..

It should have error handling on the opening of the filehandle and on retrieving the $query->param("????").

By the above, i guess u mean to have something like...

open UPLOADFILE, ">$upload_dir/$filename" || die('Couldn't open Handle');
Thanks Ron

dannyhayes
05-27-2005, 11:46 AM
If you still need help look here
http://cgi.resourceindex.com/Programs_and_Scripts/Perl/File_Management/File_Uploading/

masterofollies
05-30-2005, 02:21 AM
I changed the folders to 777. and still has a 500 error. I even added that code that was a mistake at first. I have upload.cgi in the cgi-bin folder, and have have uploads in the cgi-bin at 777, and created a uploads folder outside the cgi-bin and changed to 777. And nothing works. Im a straight beginner at cgi and perl. I know nothing basicly

Maybe we should just make the script entirely in php.

FishMonger
05-30-2005, 03:42 AM
You can change to a php script if you like however, without knowing why the Perl/cgi script is not working, you may still have the same problem with the php script or C script or a python script. Once you understand the reason, it only becomes a matter of difference in syntax.

mlseim
05-30-2005, 03:44 AM
The first post I made on this thread is an HTML form and a PHP upload
script (entirely PHP) ... no Perl involved.

I use the script all the time, it works great. I can't figure out why
you are having problems with it. Perhaps your webhost does not
have something configured correctly with your PHP?

Or, perhaps things got confusing when both PHP and Perl scripts began
showing up in the same thread.

masterofollies
05-30-2005, 04:08 AM
I copied it exactly as it was and it had 2 errors. Right now you tried a upload and it says no file selected. www.creativeskateboardteam.com/upload.html that is the form.

nkrgupta
05-30-2005, 06:23 AM
well...if u try to upload three files at one go then it shows the two errors for each of them...but if u upload only one, then it shows the error for that file and ' NO file Selected' for the other two upload boxes. I'm not a PHP guy but by the looks of the second error message--

move_uploaded_file(): Unable to move '/var/tmp/phpatZhUE' to 'uploads/OBJ-0906-024.jpg' in /home/creative/public_html/upload.php on line 16
File: OBJ-0906-024.jpg error moving file

it seems as if the file is getting uploaded to the server correctly, only at the time of writing it from the server's temp directory to your uploads directory, permission related problems are occuring (as reported in the first warning message).

may be if u post the line 16 of the code (or the whole code) then the error might be sorted out.

FishMonger
05-30-2005, 08:01 AM
I'm no php expert but I do a little work with it and I'm confussed with a portion of mlseim's script. It has 1 if clause with 3 else clauses. Shouldn't it be 1 if clause, 2 elsif, and the final one is an else clause? Maybe that's part of the reason you're receiving the php error message from the third else clause instead of one of the others.

We really need to see the EXACT and COMPLETE error message (from either/both scripts) to be able determine what to do to correct the problem.

I'm really amazed that php.net’s own site showing how to use there functions reads more like a support forum with conflicting (and in some cases bad) examples than the definitive documentation that it should have.

rwedge
05-30-2005, 08:07 AM
You may be limited by the Safe mode (http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=SNYF,SNYF:2004-46,SNYF:en&q=php+safe+mode) setting for security.

The error 'failed to open stream: Permission denied' can be a result of safe mode restrictions.

/Bob

mlseim
05-30-2005, 12:48 PM
Can't explain the errors ... hmm

I guess you'll have to go with the Perl script instead.