File Uploading & Sending EMAIL in PHP

Previously we have learnt some of the basic form elements which are used frequently while making a form. In this tutorial we will learn two of the very important tasks file uploading and sending Email. Let’s begin our discussion with file uploading in PHP.

File Uploading

Sometimes it is required that files are to be uploaded on the server for ex. You want some sort of pictures or documents as input, in such case a special form element is used.

Input type File

Syntax

<input type=”file” name=”identifier name” >

The above syntax will create a special input element which will allow user to browse for any files from their machine.

Let’s look at one example.

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select File:
    <input type="file" name="file1">
    <input type="submit" value="Upload " name="submit">
</form>

As you can see from the example, while uploading file some points should be remembered.

  • The enctype attribute of the <form> tag specifies the content-type to use when submitting form. "multipart/form-data" is used when a form requires binary data i.e. file.
  • The type="file" attribute of the <input> tag specifies that the input should be processed as a file. When viewed in a browser there will be a browse button which will allow us to select any file on computer.

How to access File?

When a form containing file as one of its input is submitted a global array $_FILES will be created. It’s a two dimensional array the 1st dimension will contain the name of the file attribute and the second dimension can be either “name” , “type” ,” size” , “tmp_name” , “error” .

You can access all those attributes like this.

  • $_FILES["filename"]["name"] –  The name of uploaded file
  • $_FILES["filename"]["type"]–  File Type
  • $_FILES["filename"]["size"] – Size in Bytes
  • $_FILES["filename"]["error"] – 0 if no error otherwise 1
  • $_FILES["filename"]["tmp_name"] – The name of temporary copy of the file stored on server

move_uploaded_file()

The move_uploaded_file() function moves an uploaded file to specified new location.

Syntax

move_uploaded_file(Source,Dest)

Source specifies the temporary name of the file uploaded on server. Dest specifies the new location where the file needs to be stored on server. If the destination file already exists, it will be overwritten.

File Upload code for allowing only PDF files less than 2 MB

<?php
    $source=$_FILES["file1"]["tmp_name"];
    $dest="uploads/".$_FILES["file1"]["name"];
    $filetype=$_FILES["file1"]["type"];
    $filesize=$_FILES["file1"]["size"]/(1024*1024);
    if($filetype=="application/pdf" && $filesize<=2)
    {
        move_uploaded_file($source,$dest);   
    }
    else
    {
        echo "Error in file Type or file size > 2MB";
    }
?>

In this example we have first stored the files temporary name to $source variable , $dest variable will allow us to upload the file in folder uploads where the file will be stored with its original name. $filetype will store the type of file which we are uploading and $filesize will convert the file size into MB. If the file type will be "application/pdf" and the file size is less than 2MB than and only than the file will be uploaded on server in uploads folder, otherwise error message will be displayed.

Sending Email in PHP

PHP uses function mail() for sending mail. The syntax for the same is given below.

Syntax

mail(to,subject,message,headers)

Parameters

  • to – specifies the email address of recipient
  • subject – specifies the subject of Email
  • message – specifies message to be sent
  • headers – its optional , specifies additional headers like Cc , Bcc.

Let’s look at one example.

<?php
    $to="pratikce87@gmail.com";
    $subject="Invitation for Party";
    $message="Hello Pratik, You are invited for party on 12/09/2017, 10.00 PM at my  place.";
    $send=mail($to,$subject,$message);
    if($send)             
    {
       echo "Mail Sent";            
    }
    else
    {
       echo "OOPS Error"
    }
?>

This is how we send a simple text email. Please note that you can send any sort of email using mail() function.

Sending HTML formatted Email

<?php
    $to="pratikce87@gmail.com";
    $subject="This is Subject";
    $message="<b>This is message</b>";
    $message.="<h1>This is heading</b>";
    $header="From:admin@discussdesk.com \r\n";
    $send=mail($to,$subject,$message,$header);
    if($send)             
    {
       echo "Mail Sent";            
    }
    else
    {
       echo "OOPS Error";
    }
?>

As you can see here we have used HTML formatting in our message, you can use any HTML code in message. $header specifies additional headers like from, Cc, Bcc. Headers should be separated with \r\n.

To conclude in this tutorial we have learnt how to upload files in PHP using move_uploaded_file() function and we have also learnt how to send Email using mail() function.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments