Jump to content

convert php form to multiple file uploading and mailing


pepper

Recommended Posts

Hey, been trying to make a form to upload an image to the server and also email the image as attachment. After nearly a week I managed to make it work. Only some validation needs to be done.

 

My next step is to make it work for multiple files, ie. 5. The form will have 5 boxes to upload images and no more. I have tried a few different things with for loops ( ie for $i=0; $i<5; $i++ {...} ) but no luck. I can't think of any solution of a loop to upload multiple files and then email multiple attachment as my code.

 

The code is:

 

<?php
session_start();
$imagename1 = $_FILES["uploadFile1"]["name"];
    $_SESSION['imagename1'] = $imagename1;

$imagename1 = $_SESSION['imagename1'];
$imagetype1 = $_SESSION['imagetype1'];
$imagesize1 = $_SESSION['imagesize1'];
$imagetemp1 = $_SESSION['imagetemp1'];


$thankyouurl = 'thankyou.php' ;
$errorurl = 'error.php' ;
$http_referrer = getenv( "HTTP_REFERER" );
$http_agent = getenv( "HTTP_USER_AGENT" );
$domain = $_SERVER['REMOTE_ADDR']; 
$filename = $_SESSION['uploadFilename'];



    if(isset($_POST['send'])) {
    
        $fp = fopen($imagetemp1, "rb");
        $file = fread($fp, $imagesize1);
        $file = chunk_split(base64_encode($file));
        $num = md5(time());
        fclose($fp);
        
    // UPLOAD FILE
    // possible PHP upload errors
    $errors = array(1 => 'php.ini max file size exceeded', 
                    2 => 'html form max file size exceeded', 
                    3 => 'file upload was only partial', 
                    4 => 'no file was attached');

    // check the upload form was actually submitted else print form
    isset($_POST['send'])
        or error('the upload form is neaded', $uploadForm);

    // check for standard uploading errors
    ($_FILES[$fieldname1]['error'] == 0)
        or error($errors[$_FILES[$fieldname1]['error']], $uploadForm);
    
    // check that the file we are working on really was an HTTP upload
    @is_uploaded_file($_FILES[$fieldname1]['tmp_name'])
        or error('not an HTTP upload', $uploadForm);
    
    // validation... since this is an image upload script we 
    // should run a check to make sure the upload is an image
    @getimagesize($_FILES[$fieldname1]['tmp_name'])
        or error('only image uploads are allowed', $uploadForm);
    
    // make a unique filename for the uploaded file and check it is 
    // not taken... if it is keep trying until we find a vacant one
    $now = date("y-m-d_H-i-s", time());
    
    while(file_exists($uploadFilename = $uploadsDirectory.$now.'_'.$_FILES[$fieldname1]['name']))
    {
        $now++;
    }

    // now let's move the file to its final and allocate it with the new filename
    @move_uploaded_file($_FILES[$fieldname1]['tmp_name'] , $uploadFilename)
        or error('receiving directory insuffiecient permission', $uploadForm);
    
        $from= 'youremail@server.co.uk'; 
        $emailTo = 'mail@hotmail.com';
        $subject = 'Quote Form'; 
        $body = "equipment_type: $equipment_type \n\nModel: $Model \n\nMake: $Make \n\nModelNumber: $ModelNumber";

        $fp = $_SESSION['fp'];
        $file = $_SESSION['file'];
        $num = "==Multipart_Boundary_x{$semi_rand}x".$_SESSION['num'];

        $headers = 'From: Name <'.$from.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
        //Normal headers
        $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=iso-8859-1\r\n";
        $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        $headers .= "".$body."\n";
        $headers .= "--".$num."\n";  
        // Attachment headers
        $headers  .= "Content-Type:".$imagetype1." ";
        $headers  .= "name=\"".$imagename1."\"r\n";
        $headers  .= "Content-Transfer-Encoding: base64\r\n";
        $headers  .= "Content-Disposition: attachment; ";
        $headers  .= "filename=\"".$imagename1."\"\r\n\n";
        $headers  .= "".$file."\r\n";
        $headers  .= "--".$num."--";        
    
        mail($emailTo, $subject, $body, $headers);
        header( "Location: $thankyouurl" );
    }

?>

 

the form  is like this

<input name="uploadFile1" id="uploadFile1" value="" class="file" type="file"/>

can anyone suggest anything please?

how can I form the for statement?

much appreciated

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.