PHP 5 File Upload

First, ensure that PHP is configured to allow file uploads.
In your “php.ini” file, search for the file_uploads directive, and set it to On:
file_uploads = On

PHP CODE: (upload.php)

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";

How to send mail from PHP Localhost Server

Yes, we can send the Email from Local Machine and Internet Connection is must.
Open your php.ini file located in your folder C:\wamp\bin\php\php5.3.4
Search for mail function in the php.ini file. Change localhost to mailer server name of your webhosting account and save it. Finally restart your local wamp server.

[mail function]
; For Win32 only.
smtp_port = 25
; For Win32 only.
sendmail_from =

How to create PHP based email form with file attachment

The HTML form with file upload box

The code for an HTML form with a file upload box is given below. User can click on the 'Browse' button to select the file from his/her local machine.




Select A File To Upload:

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:
//Get the uploaded file information
$name_of_uploaded_file =
//get the file extension of the file
$type_of_uploaded_file =
strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file =
$_FILES["uploaded_file"]["size"]/1024;//size in KBs

The code above is getting the different attributes of the uploaded file from the $_FILES[] array.
Validating the size and extension of the uploaded file

Suppose we don't want to allow files greater than the size of 100KB and we only want to allow image files to be uploaded. The validation code goes like this:
$max_allowed_file_size = 100; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");
if($size_of_uploaded_file > $max_allowed_file_size )
$errors .= "\n Size of file should be less than $max_allowed_file_size";
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The uploaded file is not supported file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); } 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. First of all we shall copy the file to a folder on the server. //copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } } 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. First, we need to include the pear library files for these classes. include_once('Mail.php'); include_once('Mail_Mime/mime.php'); The code below composes and sends the email $message = new Mail_mime(); $message->setTXTBody($text);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);

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.