PHP File Upload Example

Introduction

PHP file upload example will show you how to upload a file using PHP. In this example the file is selected using the browse button and file is uploaded to the uploads directory. PHP script stores the file into the defined location and returns response as a success or failure message from the PHP script. As a validation step I have added only to check if you have selected a file for uploading or not. The uploads directory is created under the project directory.

Prerequisites

PHP 7.0.15, HTML, Apache 2.4

Example with Source Code

Following steps are required in order to upload a file.

Creating Project Directory

Create a project directory called php-single-file-upload under your <apache server installation directory>/htdocs.

Create uploads directory under your project directory where file will be uploaded.

HTML Form

Put the below upload form fields inside <body/> tag in php page called file_upload.php.

<form name="upload_form" enctype="multipart/form-data" action="file_upload.php" method="POST">
            <label>File</label> <input type="file" id="file" name="file" /><br/><br/>
            <input type="submit" name="upload" value="Upload"/>
        </form>

Creating PHP Code

Create file_upload.php file with below source code.

First we check whether the request type is POST and upload (input button’s name) was submitted.

Then we check if any file was selected for uploading. If not selected then we show error message to select a file.

If file was selected for uploading then we check for any error and display. If there is no error then we check the file that will be uploaded already exists or not and according we show message or upload the file into the uploads directory.

<?php
			if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload'])) {
				if (isset($_FILES['file']['name'])) {
					if (0 < $_FILES['file']['error']) {
						echo '<span style="color:red;">Error during file upload ' . $_FILES['file']['error'] . '</span>';
					} else {
						if (file_exists('uploads/' . $_FILES['file']['name'])) {
							echo '<span style="color:red;">File already exists at uploads/' . $_FILES['file']['name'] . '</span>';
						} else {
							move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
							echo '<span style="color:green;">File successfully uploaded to uploads/' . $_FILES['file']['name'] . '</span>';
						}
					}
				} else {
					echo '<span style="color:red;">Please choose a file</span>';
				}
				echo nl2br("\n");
			}
        ?>

Testing the Application

Now if you run the above file then you will see below output in the browser by hitting URL http://localhost/php-single-file-upload/file_upload.php, then you will see the following output on the browser:

php file upload example

When file successfully uploaded to the directory:

php file upload example

Source Code

You can download source code.

Thanks for reading.

PHP File Upload Example

Leave a Reply

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

Scroll to top