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.
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.
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.
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.
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.
PHP uses function mail() for sending mail. The syntax for the same is given below.
Syntax
mail(to,subject,message,headers)
Parameters
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.
<?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.