Php Classroom image

Anil  Bist / Professional / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  ||
Lessons:-PHP ─ File Uploading

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.

Information in the phpinfo.php page describes the 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. These parameters are set into PHP configuration filephp.ini

The process of uploading a file follows these steps:

  • The user opens the page containing a HTML form featuring a text files, a browse button and a submit button.
  • The user clicks the browse button and selects a file to upload from the local PC.
  • The full path to the selected file appears in the text field, then the user clicks the submit button.
  • The selected file is sent to the temporary directory on the server.
  • The PHP script that was specified as the form handler in the form's action attribute checks that the file has arrived and then copies the file into an intended directory.
  • The PHP script confirms the success to the user.

As usual, while writing files, it is necessary for both temporary and final locations to have permissions set that enable file writing. If either is set to be read-only, then process will fail. 

An uploaded file could be a text file or image file or any document. 

Creating an Upload Form 

The following HTML code creates an uploader form. This form is having method attribute set to post and enctype attribute is set to multipart/form-data 

<title>File Uploading Form</title>
<h3>File Upload:</h3>
Select a file to upload: <br />

<form action="/php/file_uploader.php" method="post"
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />

Creating an upload script

There is one global PHP variable called $_FILES. This variable is an associate double dimension array and keeps all the information related to uploaded file. So if the value assigned to the input's name attribute in uploading form was file, then PHP would create the following five variables:

  • $_FILES['file']['tmp_name']- the uploaded file in the temporary directory on the web server.
  • $_FILES['file']['name'] - the actual name of the uploaded file.
  •  $_FILES['file']['size'] - the size in bytes of the uploaded file.
  •  $_FILES['file']['type'] - the MIME type of the uploaded file.
  •  $_FILES['file']['error'] - the error code associated with this file upload.

The following example should allow upload images and return the uploaded file information. 

if( $_FILES['file']['name'] != "" )
 copy( $_FILES['file']['name'], "/var/www/html" ) or
 die( "Could not copy file!");
 die("No file specified!");
<title>Uploading Complete</title>
<h2>Uploaded File Info:</h2>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>

Anil  Bist

Skills    Php

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a


  Students (0)