Nowadays, File upload and download functionality is almost mandatory. PHP provides simple and easy ways to upload and download files. You can store these files in a public folder or local storage.
In this tutorial, we will show you how you can upload files of various formats like .zip, .pdf, .docx, and .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 in your database using the 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 a 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 a PHP file to start out the program. Open your text editor or IDE and create new file called index.php or you can give name as per your preferences. Open that file and place below code:
Here, we just created a form with single input as file type and the action of this form is self. For handling form action we have included fileOperations.php.
The fileOperations.php will handle all operations related to file upload and downloads. Open file and make the following changes to it:
In the above example, first of all we are connecting to database using credentials. Then we fetch an extension from file and validate it against some predefined extensions.
We have used move_uploaded_file() function to store uploaded file in our uploads directory. It will take two parameters first one is file and the 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 the uploaded file we simply print an 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 the form then it will store the selected file on a 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 downloading functionality. Let's create a new file called downloads.php in our root directory. It will contain a simple table which will show a listing of files.
Open downloads.php file and modify it as below:
Let's create logic to perform a 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 the action column, we have created a 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 an update query and increase download count to files table against a particular file.
Conclusion
In this article, we have created file upload and download functionality in PHP where users can upload a file, see all file listing,s and download files one by one as per your requirement. In further improvement or learning purposes, you can configure file storage structures like file managers or perform delete and move action-based.