File Upload in PHP

How to upload files to the server with PHP?


The PHP method of files uploading is most commonly associated with the $_FILES command. $_FILES is an array, not a command - it's just where PHP puts the uploaded data for you.
create a form that will allow for a file to be uploaded.
This form allows you to upload a file to the server.


When the browse button is clicked, a dialog window appears and allows the user to browse their local computer for the file that will be uploaded. When the user click "Upload File" button the form is submitted and the file information is posted to the getfile.php page which contains the PHP code to upload the file to a folder located on the web server.
Coding to create a simple form. This form allows you to upload a file to the server.
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">
Filename: <input type="file" name="uploadFile">
<input type="submit" name=sub value="Upload File">
Save that form code into a file and name it as fileupload.html. If you view it in a browser it should look like above form:
Here is a brief description of the important parts of the above code:
  • enctype="multipart/form-data": Form used for file uploads must include the encode type or "enctype" attribute.

  • action="getfile.php": The name of our PHP file that will be created shortly to process the form data.
  • method="POST": Informs the browser that we want to send information to the server using POST method only ie,GET method cannot be used to send information to the server.

  • name="MAX_FILE_SIZE": Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP.

  • name="uploadFile": uploadfile is how we will access the file in our PHP script.

In PHP, file upload capability is possible using the following function:
move_uploaded_file(filename, destination)
- moves a file to a specified destination on the server.
When a file is uploaded using the move_uploaded_file() function, it is briefly stored in a temporary location on the web server. To move the file to its final destination and manipulate its various properties, we use the PHP $_FILES super global array. The $_FILES array uses the name value provided in the <input type="file" name="uploadFile"/> (in this case 'uploadFile') to identify the file being uploaded. The entries associated with the $_FILES array are described below.
  • $_FILES['UploadFile']['tmp_name']: directory on the web server where the file is temporarily stored. By default this is the uploadtemp directory located in the PHP folder.

  • $_FILES['uploadFile']['name']: name of the file on the user's system.

  • $_FILES['uploadFile']['size']: size of the file in bytes.

  • $_FILES['uploadFile']['type']: the MIME type of the file.

  • $_FILES['uploadFile']['error']:the error code associated with the file upload (0 - successful upload, 1 - file exceeds maximum upload size, 2 - file exceeds maximum file size, 3 - file partially uploaded, 4 - no file uploaded)

create a file which contains function to upload file and save it as upload.php
if ($_POST[sub] == "Upload File")
move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
"C:/Tutorial/PHP/Files/{$_FILES['uploadFile'] ['name']}");
if($_FILES['uploadFile'] ['error'] > 0)
switch ($_FILES['uploadFile'] ['error'])
case 1: echo 'File exceeded maximum server upload size';
case 2: echo 'File exceeded maximum file size';
case 3: echo 'File only partially uploaded';
case 4: echo 'No file uploaded';
echo 'File successfully uploaded';

In this code "move_uploaded_file" is a function built in to PHP is used to file upload in PHP. When the file was uploaded, PHP create a temporary copy of the file, and built an array containing information about the file. $_FILES is that array.
The move_uploaded_files function takes two parameters, contained inside parentheses and separated by a comma. The first parameter tells the function where to get the file and the second tells where to move the copy.
Next we refer to the 'tmp_name' associative index, which provides us with the name of the temporary file created by PHP.
we tell the move_uploaded_file function to create a copy of the original file in a directory called "C:/Tutorial/PHP/Files/" within the website, and to name the file with its original name. If you use linux system you can mention directory path as /home/user/ ie, you can change the directory path as you required the file to be uploaded.

PHP Topics

Ask Questions

Ask Question