How to Download or upload files in PHP with example

Nowadays, File upload and download functionality almost mandatory. Laravel provide simple and easy ways to upload and download files. You can store these files into public folder or local storage.

In this tutorial, we will show you how you can upload files of various formats like .zip, .pdf, .docx, .ppt, as well as image files through a form using PHP to be stored in a folder on our server. Here, we will also store file information like name, size and time to our database.

Before starting let's create a table into your database using below query. In this table, we will store information about uploaded file and manage download counts by this table.

Let's start our article by creating new directory in our localhost by fileUploadDownload. Here, we will also use sub directory called uploads to store uploaded files

Uploading File In PHP

Let's create PHP file to start out program. Open your text editor or IDE and create new file called index.php or you can give name as per your prefrences. Open that file and place below code:

Here, we just created form with single input as file type and action of this form is self. For handling form action we have included fileOperations.php.

The fileOperations.php will handle all operation related to file upload and downloads. Open file and make following change to it:

In above example, first of all we are connecting to database using credentials. Then we fetch extension from file and validate it against some predefined extensions.

We have used move_uploaded_file() function to store uploaded file to our uploads directory. It will take two parameters first one is file and second is storage location.

Lastly, we added record into our files table. It will stores file size and file name. In any case, script face error while moving uploaded file we simply print error message.

Now you can test file upload functionality by executing index.php file in your browser. Open your browser and enter below url :

Here, it will show form with single input and when you select file and submit form then it will store selected file to server. It will also create record in files table. Right now you can check it using phpMyAdmin.

Downloading File In PHP

After adding some files, now we can start download functionality. Let's create new file called downloads.php into our root directory. It will contain simple table which will show listing of files.

Open downloads.php file and modify as below:

Let's create logic to perform download using action button. Open filesLogic.php and append below code into it:

First of all, we fetch all data from files table and print it into table. In action column, we have created URL for the same page with table id as a parameter. So it will create a unique link for the file. When the user clicks on that link it will start implementing download logic.

In Download logic, first of all we will find records from files table and then create path using name value. Here, we have set some headers for files and started downloading file.

At last, we perform update query and increase download count to files table against particular file.


In this article, we have created file upload and download functionality in PHP where users can upload a file, see all file listing and download files one by one as per your requirement. In further improvement or learning purposes, you can configure file storage structure like file managers or perform delete and move action-based.

Share your thoughts

Ask anything about this examples