while defining the <form> tag. This is to tell the browser that this form will be used to upload files. Then we have added the “name” and “email” fields to collect the user info. The third form field is the file upload box.

<input type="file" name="uploaded_file">

On hitting the “Submit” button, the form data along with the file data is posted to the script pointed to by the ‘action’ attribute of the form.

Getting the uploaded file in the PHP script

In the PHP script, we will first validate the submission and if the validation succeeds, we will send the submission by email.

We can access the uploaded file and its different attributes by using the $_FILES array. This array will contain the name, size, path and other attributes of the uploaded file. The code below gets the name, type and size of the uploaded file:

In the above code we are validating the file size and type. We have the maximum allowed file ($max_allowed_file_size) size set to 100KB. The $allowed_extensions array cotains the file extensions of all allowed file types.
The validation code checks to see whether the file extension matches any of the extensions in the $allowed_extensions array.

If there are errors found in the validation, the error is displayed. Else we proceed with sending the email.

Copy the uploaded file

Now, its time to send the uploaded file with the user message to the recipient’s email address.

This code copies the uploaded file to the ‘uploads’ folder. You can change the uploads folder by updating $upload_folder.
Please make sure that “uploads” folder has “777” permissions.

Sending the Email

The next step is to compose and send the email. We will use the Pear library for composing and sending the email. ( see the Pear installation instructions below ) The pear classes PEAR::Mail and PEAR::Mail_Mime are used for sending the email with the attachment.

Mail_mime() class helps in composing a MIME message. In the code above, a Mail_mime object is created, the text body is updated ( $message->setTXTBody($text); ) and the attachment is added ( $message->addAttachment(file) )

The MIME encoded message is then sent using the Mail class.

The sample PHP upload form

The download contains a complete PHP upload form that sends the uploaded by email.

How to Install the PEAR Library

In this article we used the PEAR::Mail and PEAR::Mail_Mime classes to send the email with attachment. Before using these classes, you need to install the PEAR package on your server. It is beyond the scope of this tutorial to discuss the installation of PEAR. But, I want to give you a quick tip. Get the PEAR installer script from

Uhhh, I can’t belive how many stupid people are trying to write PHP…
Just hear this “Do you have PHP installed?” , “I uploaded the pear script, but it doesn€™t €œrun€. When I go to it in my browser, it just shows the script as text. How do I get it to run?”

Anyway your clever to choose PEAR for this job, the rest is bullshit, nice, nice, nice!

Mubshir Gilani …. i have a question how did you get it to work i’m getting a not found in error message [ include_once(Mail_Mime/mime.php) [function.include-once]: failed to open stream: No such file or directory ]

For those having problems with PEAR and the Modules. These will need to be either installed on your production server using the script as suggested http://pear.php.net/go-pear. If using virtual hosting or some other hosting environment your hosting company may well supply you with the include paths required as pear will probably already be installed.

Jason
Possibly your path to the pear directory is wrong. From a command line type
pear config-show
to get your correct paths…

hello, first thanks for the script.
i’m having a hard time to make the script work, i see that it does upload the file to the uploads dir but it won’t send the email. after hitting submit all i get is a blank page with number 0.
i’ve check that the server does have mail and mail_mime installed.
any ideas?

Hi, your script is exactly what I need but unfortunatley is not quite working. The form is there but when I click submit ti then goes to an error page advising that the url could not be found. Any help would be appreciated. Alternatively if you need other info then please contact me. Cheers