File Uploading & Sending EMAIL in PHP

Manas Singh 01st Oct 2020

Previously we have learned 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 Emails. 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 forex. You want some sort of pictures or documents as input, in such cases, 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 that will allow the 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 files some points should be remembered.

  • The type attribute of the <form> tag specifies the content-type to use when submitting the 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 that will allow us to select any file on the computer.

How to access the File?

When a form containing a 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 the server. Dest specifies the new location where the file needs to be stored on the 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 the server in the 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 the recipient
  • subject – specifies the subject of Email
  • message – specifies the message to be sent
  • headers – its optional, specify 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 the 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 the message. $header specifies additional headers like from, Cc, Bcc. Headers should be separated with \r\n.

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

Authored By Manas Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk