php file upload and mail - convert to multiple file

otto

New Member
Messages
1
Reaction score
0
Points
0
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 it sort of works. 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:
<?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
HTML:
<input name="uploadFile1" id="uploadFile1" value="" class="file" type="file" />
can anyone suggest anything please?
how can I form the the for statement?
much appreciated
 
Top