Agree with: How to create downloadable pdf in php and Mysqli
Free download kristen bjorn Men & Uniforms | |
Kodak black project baby 2 free download | |
Google drive download file? |
How to upload and download files PHP and MySQL
This tutorial demonstrates how you can upload files of various formats including .zip, .pdf, .docx, .ppt, as well as image files through a form using PHP to be stored in a folder on our server.
We will also record the name of the uploaded files and related info such as the file name, size, and the number of downloads in a database table.
Create a new PHP project folder and call it file-upload-download. Create a subfolder inside this folder called uploads (this is where our uploaded files will be stored), and a file called diseinuak4web.net
diseinuak4web.net is where we will create our file upload form. Open it and put this code inside it:
diseinuak4web.net:
It's a very simple form that takes just the input field for our file and an upload button.
In the head section, we are linking to our diseinuak4web.net file to provide some styling to our form. Create that file in the root of our application and add this CSS code to it:
diseinuak4web.net:
At the top of diseinuak4web.net, we are including diseinuak4web.net file. This is the file that contains all the logic of receiving our submitted file and saving it to the uploads folder as well as storing the file information in the database. Let's create this file now.
diseinuak4web.net:
At the top of this file, we are connecting to a database but we've not yet created it yet. Let's do that now.
Create a new database called file-management. Under this database, create a table called files and give it the following fields.
- id - INT
- name - VARCHAR()
- size - INT
- downloads
Now open diseinuak4web.net file in your browser. For me, I'll head over to http://localhost/file-upload-download/diseinuak4web.net
Click on the file input field and select any file from your machine to upload.
Note: Depending on your php configuration, your file may fail to upload if the size exceeds the upload_max_filesize value set in your diseinuak4web.net file. You can always configure this info in your diseinuak4web.net file. Increase the values of post_max_size and upload_max_filesize .
Having selected your file, you can click on the upload button. If everything goes well, your file will be uploaded to the uploads folder in your project and a new record will be created in the files table in the database containing the filename, size, and downloads count.
Now our file has been uploaded. You can check your uploads folder and database table to confirm that it was successful. Let's display it so that the user can view it and click on it to download it. First, we need to fetch the file info from the database.
Open diseinuak4web.net and add these 3 lines of code just below the line where we connect to the database:
This selects all files information from the database and sets it to an array variable called $files.
Now create a file called diseinuak4web.net in the root folder of our application and add this code inside it:
diseinuak4web.net:
Now on this page, the files information from the database are listed each along with its size in KB and number of downloads. There is also a download button against each file. What remains now is the code that actually downloads the file from our uploads folder. Let's write the code right away.
Open diseinuak4web.net again and add this code at the end of the file:
diseinuak4web.net:
When we were listing the files, each download button (or rather, download link) had a parameter called file_id attached to it. So when you click on the download link of a file, that file's id is sent to the diseinuak4web.net page and is grabbed by this piece of code we just added now.
The code then fetches that particular file info from the database using the file_id parameter and then stores the file info in a variable called $file. Using PHP's file_exists() method with the full path to our file as an argument we check that the file actually exists in our uploads folder. Then we proceed to set some headers and finally respond with the file to the user using the readFile() function in PHP.
After the file is downloaded, we update the downloads count for that particular file in the database.
Conclusion
That's about it with file upload and download. You can further customize it to build cool PHP applications. Thanks very much for following. Let me know what you think about this article in the comments section below, if you please.
Have a nice time!
Awa Melvine
Источник: [diseinuak4web.net]
-
-
-