PHP MySQL tutorial

File Upload in PHP

Uploading files to server via website is a common task. While registering your self on some website, you are often required to upload your photo or CV, such tasks are performed via file upload functionality in PHP. This article explains how to upload a file to PHP server.

First of all, create an HTML form with a file upload functionality. The code for the HTML form is as follows:






Select a file to upload:

Download the code
Run the code

The HTML form contains an attribute enctype with value “multipart/form-data”. This attribute encrypts the uploaded file. When the file is uploaded and the submit button is clicked, an HTTP POST request will be sent to “Php-fileupload.php” page. The code for this page is shown below



The code for Php-fileupload.php file is simple. The directory “uploadedfiles/” has been defined at the top. This is the directory where your files will be uploaded on the server. You must create this directory in the root directory of your Php-fileupload.php file.

The path for the uploaded file is being created by appending the directory name with the file name. Now, when the user uploads the file and click the submit button, “move_uploaded_files” function is triggered which contains name of the file and the target path as parameter. If file is successfully uploaded, this method returns true, otherwise it returns false. In the above code, if the file is successfully uploaded, a message is displayed to the user that file has been uploaded. Otherwise, if due to any reason file upload is failed, user prompted with the message that file could not be uploaded.

<<< Variable Examination Functions in PHPPHP and AJAX >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .