SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using $_FILES to send images via email

    Hi there,

    I have a form where people can make a booking for an advertisement, and attach up to four image files that will be used in their ad.

    The way I have it set up now is that the first page contains the form which, when submitted, gets sent to the second page, where the input from the form is displayed to the user for confirmation. I am storing the user input as session variables.

    If all their data is correct, they submit again, which goes to the final page, which 'processes' the form by putting together an email, and sending me their info.

    Now, the first form has file input fields, which I know are accessed using $_FILE. I have stored this into a Session variable (on the second page), like this -

    $_SESSION['fileatt01'] = $_FILES['fileatt01'];

    so that I can access the file info by saying $_SESSION['fileatt01']['type'] etc, on the final 'processing' page.

    On that final processing page, I'm trying to put together the email like this:

    PHP Code:
        $headers "From: $from";

        
    $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/html; charset=\"iso-8859-1\"\n" .
                    
    "Content-Transfer-Encoding: 7bit\n\n" .
                    
    $themessage "\n\n";

        
    // image 1
        
    $fileatt $_SESSION['fileatt01']['tmp_name'];
        
    $fileatt_type $_SESSION['fileatt01']['type'];
        
    $fileatt_name $_SESSION['fileatt01']['name']; 
    //    if (is_uploaded_file($fileatt)) {
        // Read the file to be attached ('rb' = read binary)
        
    $file fopen($fileatt,'rb');
        
    $data fread($file,filesize($fileatt));
        
    fclose($file);

        
    // 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-Transfer-Encoding: base64\n\n" .
                
    $data "\n\n"
    I commented out the (is_uploaded_file) part, because the file is not coming through a post variable.

    I then get these errors when trying to process the final page:

    Warning: fopen(C:\WINDOWS\TEMP\php1B2.tmp) [function.fopen]: failed to create stream: No such file or directory in C:\home\blah\public_html\addyouritem\processclothing.php on line 151

    Warning: filesize() [function.filesize]: Stat failed for C:\WINDOWS\TEMP\php1B2.tmp (errno=2 - No such file or directory) in C:\home\blah\public_html\addyouritem\processclothing.php on line 152

    Warning: fread(): supplied argument is not a valid stream resource in C:\home\blah\public_html\addyouritem\processclothing.php on line 152

    Warning: fclose(): supplied argument is not a valid stream resource in C:\home\blah\public_html\addyouritem\processclothing.php on line 153
    I hope somebody can help me pinpoint what is going on here, and why I am getting these errors??!!! The email comes through properly, but not with the attachment.

    Thanks very much if you can help

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uploaded files are removed from tmp once the script ends. You'll have to move it somewhere permanent once the upload script is done on the first page.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaargh, that is what I was hoping not to hear I couldn't find any documentation on how long it stays there....

    Ok, so I'm going to have to move it to another directory, access it from that location for the email, then remove it after I'm done.

    Hmm, it's off to work I go


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •