cancel
Showing results for 
Search instead for 
Did you mean: 

photo uploads

beermatt
Member

Hi, I would like to create a simple way for people to send me photos via a webpage.  I have tried a script which I have placed in my cgi-bin called upload.pl

 

 

1 #!/usr/bin/perl
2 use CGI;
3 my $cgi = new CGI;
4 my $dir = $cgi->param('dir');
5 my $file = $cgi->param('file');
6 $file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
7 my $name = $2;
8 open(LOCAL, ">$dir/$name") or die $!;
9 while(<$file>) {
10    print LOCAL $_;
11 }
12 print $cgi->header();
13 print "$file has been successfully uploaded... thank you.\n";

 

 

The page in question is at www.beermattonline.co.uk/pink/contact_pets.html

 

It doesn't seem to work.  Would anyone be able to help me out?

 

Thanks in advance, Matt.

1 ACCEPTED SOLUTION

Accepted Solutions

dave
Guru

Check that the target path is correct... save the following to a file, upload it to your target directory and open it in a browser. This will show the correct path name for that directory (it may be /webpages2 rather than /webpages)

 

 

<?php
echo dirname(__FILE__);
?>

 

I think you also need to change the following line on your form:

 

<INPUT TYPE="FILE" NAME="file">

 to:

 

 

 

<input name="uploaded" type="file">

 

 

If it is still failing, try running the uploader.php from a normal directory rather than from cgi-bin.

 

View solution in original post

4 REPLIES 4

dave
Guru

Here is a php version that is known to work. Save this as uploader.php or something similar and point your form action to this:

 

Change the target path to wherever you want the files to save to.

 

 

<?php 
$target = "/services/webpages/b/e/beermattonline.co.uk/public/uploads/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

 

 

beermatt
Member

Hi Dave.  Thanks for your help.  That still doesn't seem to work.  Here is what I have on my page followed by the php version...  I have changed the permission for the php file to 755.  Any other suggestions?

Thanks again, Matt.

 

 

<FORM ENCTYPE="multipart/form-data" ACTION="/cgi-bin/uploader.php" METHOD="POST">
<p class="contact">
please select a file to upload </p>
<INPUT TYPE="FILE" NAME="file">
<p>
<INPUT TYPE="submit">
</FORM>

"<FORM ENCTYPE="multipart/form-data" ACTION="/cgi-bin/uploader.php" METHOD="POST"><p class="contact">please select a file to upload </p><INPUT TYPE="FILE" NAME="file"><p><INPUT TYPE="submit"></FORM>"

 

 

<?php 
$target = "/services/webpages/b/e/beermattonline.co.uk/public/pink/pet_photos/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

<?php $target = "/services/webpages/b/e/beermattonline.co.uk/public/pink/pet_photos/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";} else {echo "Sorry, there was a problem uploading your file.";}?>

 

dave
Guru

Check that the target path is correct... save the following to a file, upload it to your target directory and open it in a browser. This will show the correct path name for that directory (it may be /webpages2 rather than /webpages)

 

 

<?php
echo dirname(__FILE__);
?>

 

I think you also need to change the following line on your form:

 

<INPUT TYPE="FILE" NAME="file">

 to:

 

 

 

<input name="uploaded" type="file">

 

 

If it is still failing, try running the uploader.php from a normal directory rather than from cgi-bin.

 

beermatt
Member

Many thanks.  That seems to work.  For reference I am not sure if all three steps were necessary but moving the uploader.php file to the root folder did the trick.  I am going to look for a way to include an email address with the uploaded file and to give the visitor a way to return to a page on the site after upload.  At least I know that it will work now.

Many thanks Dave. Kudos.

Matt.