Page 1 of 1

Photo upload form help

Posted: Mon Feb 16, 2009 10:12 am
by colinhall
Hi there,

I'm currently helping maintain a website, and I've been asked to develop an upload form for a competition...

The general idea is pretty basic...the user uploads an image, along with their name and info. The image is stoed on the server, and the other info is sent to an email address.

The way I'm trying to make it, is so that the user uploads their image to the server (I can do this perfectly fine) and is taken to another page, which immediately displays their image, and has a form for their email address, name, number, etc

This form will be emailed to a specific email address (along with the file name)


I know this may be a fairly roundabout way of doing things, but I'm at a loss with the whole MySQL database idea.

Has anyone got any ideas/tips/help/feedback on what I'm doing (well, *trying* to do...)


Any help ill be greatly appreciated!!

Thanks,
Colin


FYI, this is the code I currently have for the 2nd form page (the page shown after the file is uploaded)
Everything works fine and it gets to this page, but I can't quite get my head around the IMG SRC on the 4th from bottom line...I have a semi-idea that its to do with the <? being closed, but I'm not at a high enough level right now to be sure/fix it (I can happily admit I'm still learning)


<?php
if ((($_FILES["file"] ["type"] == "image/pjpeg")
|| ($_FILES["file"] ["type"] == "image/jpeg"))
&& ($_FILES["file"] ["size"] < 1048576))
{
if ($_FILES["file"] ["error"] > 0)
{
echo "Return Code: " . $_FILES["file"] ["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"] ["name"] . "<br />";
echo "Type: " . $_FILES["file"] ["type"] . "<br />";
echo "Size: " . ($_FILES["file"] ["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"] ["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"] ["name"] ))
{
echo $_FILES["file"] ["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"] ["tmp_name"] ,
"upload/" . $_FILES["file"] ["name"] );
echo "Stored in: " . "upload/" . $_FILES["file"] ["name"] ;
}
}
}
else
{
echo "Invalid file";
}
?>
<img src="upload/ . $_FILES["file"] ["name"] . ">
<p>&nbsp;</p>
<p><strong>Fill out the form below to validate your details!</strong></p>
<div id="content">

Re: Photo upload form help

Posted: Mon Feb 16, 2009 10:19 am
by mattpointblank
You need to open PHP to use its variables:

Code: Select all

 
<img src="upload/<?php echo $_FILES["file"] ["name"]; ?>">
 

Re: Photo upload form help

Posted: Mon Feb 16, 2009 10:29 am
by colinhall
Ahh perfect!!

I knew it wouldn't be anything too tricky...I've never touched PHP before so I'm learning at the same time as implicating...thansk for the help!

Colin

Re: Photo upload form help

Posted: Tue Feb 17, 2009 3:21 am
by colinhall
Sorry for double post, but there's one more thing...

I'm trying to send information about the uploaded image in a form...the form works fine (I receive the appropriate email with name, email and company name), however, I'm having trouble trying to embed the file/info of the file within this email.

The content of the email form currently looks like this:
---------------------------------------------------

Code: Select all

$to = "**********@gmail.com";
$from = Olympus Competition;
$subject = "Competition";
$name = $_POST["name"];
$email = $_POST["email"];
$company = $_POST["company"];
 
$message1=<<<HD
               
Name: $name
Email: $email
Company: $company
---------------------------------------------------

Is there any way to add something along the lines of:

Code: Select all

<img src="upload/<?php echo $_FILES["file"] ["name"]; ?>
so I receive the image in an email?
If not, is there a way so I can be sent a link to the image, or even just the filename?

Re: Photo upload form help

Posted: Tue Feb 17, 2009 4:19 am
by mattpointblank
You can only use the $_FILES["file"] ["name"] variable when the file is still in memory, eg, when the user has just uploaded it. Normally it's typical to store the filename in a database, so then you can refer to it at a later date. Sort that out first, then look into attachments with emails. Here's some (fairly messy) code I use for that:

Code: Select all

 
$to      = "you@site.com";
$from    = "someone";
$subject = "subject";
$message = "test";
 
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$fileatt_size = $_FILES['fileatt']['size'] / 1024; //filesize in KB
 
 
$file = fopen($fileatt,'rb');
      $data = fread($file,filesize($fileatt));
      fclose($file);
    
      // Generate a boundary string
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      
      // Add the headers for a file attachment
      $headers .= "\nMIME-Version: 1.0\n" .
                  "Content-Type: multipart/mixed;\n" .
                  " boundary=\"{$mime_boundary}\"";
    
      // Add a multipart boundary above the plain message
      $message = "This is a multi-part message in MIME format.\n\n" .
                 "--{$mime_boundary}\n" .
                 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                 "Content-Transfer-Encoding: 7bit\n\n" .
                 $message . "\n\n";
    
      // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                  $data . "\n\n" .
                  "--{$mime_boundary}--\n";
    }
    
    // Send the message
       mail($to, $subject, $message, $headers);