How to Upload File in PHP

A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.

HTML form is used to upload file on server in PHP. Normally files are uploaded into a temporary directory and then relocated to a target destination using a PHP script.

In the phpinfo.php all information are describes temporary directory that is used for file uploads as upload_tmp_dir and the maximum permitted size of files that can be uploaded is stated as upload_max_filesize.
For uploading a file usd following steps :

First opens the page which containing a HTML form with text files, a browse button and a submit button.

Then clicks on the browse button and selects a file to upload to PC

The full path to the selected file appears in the text filed then clicks on submit button.

Selected file is store to the temporary directory on the server.

The PHP script that was used in the form’s action attribute checks that the file has arrived and then copies into a directory.


Follow some rules when upload file using HTML form

First sure it that the form uses “POST” method
The form also needs the form attribute: enctype=”multipart/form-data”. It specifies which content-type to use when submitting the form
file upload will not uploaded without the requirements attribute


<form name="form" action="" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>

$errors= array();
$file_name = $_FILES['filename']['name'];
$file_size =$_FILES['filename']['size'];
$file_tmp =$_FILES['filename']['tmp_name'];

$expensions= array("jpeg","jpg","png");

if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";

if($file_size > 2097152){
$errors[]=’File size too large’;

echo "File Uploaded Successfully";

Leave a Reply

Your email address will not be published. Required fields are marked *