PHP File Upload Example

Introduction

PHP file upload example will show you how to upload a single file using PHP programming language. In this example the file is selected using the browse button and file is uploaded to the uploads directory. Make sure the uploads directory exists under your root project 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.

You may also like to read:

Prerequisites

PHP 7.4.3, Apache HTTP Server 2.4

Project Directory

Create a project directory called php-single-file-upload under your <apache server installation directory>/htdocs folder. Create uploads directory under your project directory where file will be uploaded.

HTML Form

You need to create an upload form where you need to give your users an option for selecting a file for uploading to the server. Put the below upload form fields inside <body/> tag in the page 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>

PHP Code for File Upload

Create file_upload.php file with below source code.

First I have checked whether the request type is POST and upload (input button’s name) was submitted by the user.

Then I have checked if any file was selected for uploading. If not selected then I show error message to select a file.

If a file was selected for uploading then I checked for any error and display. If there was no error then I checked the file that will be uploaded already exists or not and according I 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 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

Download

Leave a Reply

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