In this chapter, we would learn how to upload file to the server using PHP script. Similar to sending text data, you also use a form to upload files. We would cover the following three topics:
1. How File Upload Works in PHP
The phpinfo() page specifies the temporary directory where files are uploaded to. This is given by the upload_tmp_dir parameter. The maximum file size permitted for upload is given by the upload_max_filesize parameter. These parameters are set in the php.ini configuration file.
File upload takes place by the following process:
- a HTML form is provided with a browse button and a submit button
- the user clicks on the browse button and selects the file from the local computer to upload. The path to the selected file is displayed in the text field
- the user clicks on the submit button
- the selected file is transferred to a temporary directory on the server
- then the php script handling the file submission sees the file in the temporary folder and transfers it to the intended directory
- a file upload success message is sent to the user
It is also to note that for the file to upload successfully, the upload PHP script should have write permission on those locations. The files being uploaded are available in the $_FILES associative array.
2. Creating an Upload Form
The HTML form below creates an upload form. This form is method attribute must be set to POST. Also the enctype attribute is set to multipart/form-data. The form below allows for image upload
<?php if(isset($_FILES['photo'])){ $errors = array(); $fileName = $_FILES['photo']['name']; $fileSize = $_FILES['photo']['size']; $tempDir = $_FILES['photo']['tmp_name']; $fileType = $_FILES['photo']['type']; $fileExt = end(explode('.', $_FILES['photo']['name'])); $extensions = array('jpg', 'png', 'jpeg'); if(in_array($fileExt, $extensions) === false){ //validate the extension $errors[] = "Unknown file extension"; } if($fileSize > 3023124){ //validate the size $errors[] = "File size must not exceed 3 MB"; } if(empty($errors) == true){ move_uploaded_file($tempDir, "uploads/".$fileName); echo "File successfully uploaded"; } else { print_r($errors); } } ?> <html> <form action="" enctype="multipart/form-data" method="POST"> <input type="file" name="photo" /> <input type="submit" /> </form> </html>
This code displays a file upload form and allows the user to select a file and submit.
the end() function returns the last elements of an array
the explode() function splits a string into parts using a specified separator. In the code we use this function to split the filename and obtain the part following the dot(.).
3. Creating a File Upload Script
Notice that in the code above, we used a variable called $_FILES. This is a global variable that is an associative array of all the information related the file being uploaded. Assuming the name of the file upload input is image, then the following 5 variables would be created by PHP.
- $_FILES[‘image’][‘tmp_name’] − the uploaded file in the temporary directory on the server.
- $_FILES[‘image’][‘name’] − the actual name of the file uploaded.
- $_FILES[‘image’][‘size’] − the size in bytes of the file uploaded.
- $_FILES[‘image’][‘type’] − the MIME type of the file uploaded.
- $_FILES[‘image’][‘error’] − the error code associated with the file upload.
As an exercise for you, write create a file upload form and also display the information of the file being upload on the web page.