Jump to content

Form / Image upload field


nikola
 Share

Recommended Posts

I have a job application form that needs to be updated with an upload field that will accept only images (jpg, gif, png).

I need the following function:

When users will submit the form, the form will send gathered information in the body of the e-mail and the uploaded image as an attachment.

The form functions just fine without image upload field.

I have this code right now in my form (I've stripped out everything except what isn't working):

<?php

if(isset($_POST['submit'])) {

$filename=$_FILES["file"]["name"];
$filetype=$_FILES["file"]["type"];
$filesize=$_FILES["file"]["size"];
$filetemp=$_FILES["file"]["tmp_name"];

$recipient = "mail@mail.com";
$subject = "Subject";
$message = "Message";

$fp = fopen($filetemp, "rb");
$file = fread($fp, $filesize);

$file = chunk_split(base64_encode($file));
$num = md5(time());

//Normal headers

$headers  = "From: $email\r\n";
$headers  .= "MIME-Version: 1.0\r\n";
$headers  .= "Content-Type: multipart/mixed; ";
$headers  .= "boundary=".$num."\r\n";
$headers  .= "--$num\r\n";

// This two steps to help avoid spam

$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

// With message
   
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n"; 

// Attachment headers

$headers  .= "Content-Type:".$filetype." ";
$headers  .= "name=\"".$filename."\"r\n";
$headers  .= "Content-Transfer-Encoding: base64\r\n";
$headers  .= "Content-Disposition: attachment; ";
$headers  .= "filename=\"".$filename."\"\r\n\n";
$headers  .= "".$file."\r\n";
$headers  .= "--".$num."--"; 

mail($recipient, $subject, $message, $headers) or die("Error!");

fclose($fp);

}

Also I don't have the part that checks and allows only images to be uploaded.

Maybe it needs access to temp folder and I don't know how to make this work...

Link to comment
Share on other sites

What's missing here is to move your file from $filetemp to $path + $filename. Once you've confirmed that the $filename is consistent with the format and extension you are expecting, and the size is consistent with a size you are willing to email, then you'll want to use PHP's move_uploaded_file($filetemp, $path . $filename) to the destination. If this is coming from a non-administrative upload form, you'll want your destination to be non web accessible. That means a directory off your /home/ or a directory blocked by apache. In a PW installation, everything is blocked from /site/assets/cache/, so that is a possibility. Here's how you might do it:

<?php
if(isset($_POST['submit']) && !empty($_FILES['file']['size'])) {

    $filename = $_FILES["file"]["name"];
    $filetype = $_FILES["file"]["type"];
    $filesize = $_FILES["file"]["size"];
    $filetemp = $_FILES["file"]["tmp_name"];

    if($filesize < 10) throw new Exception("File too small");
    if($filesize > 1000000) throw new Exception("File too big"); 

    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); 
    if(!in_array($ext, array('jpg', 'gif', 'png'))) throw new Exception("Invalid file type"); 
    $destination = $config->paths->cache . $user->name . '.' . $ext; 
    if(!move_uploaded_file($filetemp, $destination)) throw new Exception("Unable to move uploaded file"); 
    
    // fopen $destination and encode for your email
    // and remember to delete the file when done:

    unlink($destination); 

    // then send your email

}

Written in the browser, so may need tweaks. Also, the messages posted this week from this thread may be helpful too:

http://processwire.com/talk/index.php/topic,83.0.html

Link to comment
Share on other sites

Ryan,

thanks a lot, will try it tomorrow. I'll let you know how it went.

Also, I'm close to completion of new admin theme for PW, will post it by the end of the week. My little contribution back to the community...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...