The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Attaching a File to a Form

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
  • #27444

    Should be pretty simple. I need to create a form that allows the user to attach a doc and send it to a specified email address. I know how to make a mail form. However, I’ve never had the pleasure of allowing people to add attachments. Looking for some "how to" or tutorials on this. Any point in the right direction would be appreciated. Thanks!


    To give you some pointers you would have to upload the document first as the web server have no access to the local machine to be able to pick it up.
    Then you need to attach it to the email and send it.
    Now you need to clear down the uploaded file.

    To upload a file

    To send the attachment

    Hope this helps


    I got the form to accept an attachment and send the attachment but if it’s a doc it’s blank:

    $fileatt = $_FILES[‘file’][‘tmp_name’];
    $fileattType = $_FILES[‘file’][‘type’];
    $fileattName = $_FILES[‘file’][‘name’];
    $file = fopen($fileatt,’rb’);
    $data = fread($file,filesize($fileatt));

    $semi_rand = md5(time());
    $mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

    $headers = “from: $email”;

    $headers .= “nMIME-Version: 1.0n” .
    “Content-Type: multipart/mixed;n” .
    ” boundary=”{$mime_boundary}””;

    $content = “Info about user”;

    $content = “This is a multi-part message in MIME format.nn” .
    “–{$mime_boundary}n” .
    “Content-Type: text/plain; charset=”iso-8859-1″n” .
    “Content-Transfer-Encoding: 7bitnn” .
    $content . “nn”;

    $data = chunk_split(base64_encode($data));

    $content .= “–{$mime_boundary}n” .
    “Content-Type: {$fileattType};n” .
    ” name=”{$fileattName}”n” .
    “Content-Disposition: attachment;n” .
    ” filename=”{$fileattName}”n” .
    “Content-Transfer-Encoding: base64nn” .
    $data . “nn” .

    $send = mail( “[email protected]”, “Form”, $content, $headers);
    header(‘Location: success page’);
    header(‘Location: fail page’);

    The email sends correctly, there is an attachment with the correct name and file type. when it opens it is empty. An image will send with no problem.

    I got the tutorial from sitepoint: … mail-php/5


    I downloaded the test files from Sitepoint and ran them, their doc files are blank too. Problem is I’ve compared it to other examples and they are virtually identical.


    Well. Just goes to show you can’t trust anyone.

    I have some code but I’m not in a position to get at it and won’t be for a few hours.
    I’ll post it when I can.


    That would be great! I want to learn how to do this so I can just hammer it out in the future since I know I’ll be asked to do this again. Thanks for your help


    Hi, Here to post my code as requested.

    The form looks like this. Nothing special I’m sure you’ll agree.


    Click browse to find the image you wish to upload.
    The file size cannot exceed 1 MB

    The processing PHP bit looks like this

    1000000) {
    echo “File size is bigger than 1 MB. It must be rezised in order to upload.”;
    @copy(“$img2”, “/THIS IS MY SERVER PATH/$img2_name” )
    or die(“Upload Failed!
    } else {
    die(“No input file!.

    echo “Image Uploded”;

    Where I have "THIS IS MY SERVER PATH" you need to add in the actual path to your upload folder on your server. I’m sure you already know this but here we are in any case.
    I have found the best way to find it is to force a PHP error say by removing a ; and the running the script. The path is output on the error page.

    Incidentally I have just tested this and it worked fine for a .doc file. Uploaded it then I FTP’d it back to my machine and opened it without a problem.

    If this doesn’t work then you may have to start looking at your server setup. I’m not expert with the setup but that may be where the problem lies.


    Is there anyway to send an attachment without uploading to the server? I’ve tried PHPMailer and I can’t get anywhere with it. I tried SwiftMailer and our mail server doesn’t like it.


    I tested the script on an offsite location outside of our web server and it works. docs, images, etc all send and have content. 100% perfect.

    In the offsite script i left the


    is there a setting on the webserver that would prevent the data from being in the file?


    The headers are night and day. The offsite server has all the header data. Our server only shows:

    Received: from ctc03 ( by (xx.xx.x.xx)
    with Microsoft SMTP Server id; Wed, 13 Jan 2010 12:05:25 -0500
    Date: Wed, 13 Jan 2010 12:05:24 -0500
    Subject: Test
    To: [email protected]
    From: <[email protected]>
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    Message-ID: <[email protected]>
    Return-Path: [email protected]

    Is there anyway to send an attachment without uploading to the server? I’ve tried PHPMailer and I can’t get anywhere with it. I tried SwiftMailer and our mail server doesn’t like it.

    Not as far as I know. The problem with that concept is that the browser, and therefore the internet, is sandboxed from the rest of your computer. THis basically means that the browser has no access to the file to be able to attach it to the email.

    Just imagine it in a larger scale. Effectively you’re running a program (you PHP) on a computer in a different location, over the web. Compare this to trying to email a file that is on the computer upstairs from the computer downstairs. It’s not there is it?

    I hope that’s clear and if it’s too simplified I apologize.

    So it’s all working then?

    Don’t forget to clear down the temp folder that you’re uploading to.


    I have it working now from an offsite server. If I send using the companies server it strips the header and only the file name and type come through. Offsite the whole file sends, no issues. I never had to upload it to the server.


    For mail forms there is a special tutorials resource – On it you can find examples of usage of forms and other information.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Back End’ is closed to new topics and replies.