Script to download documents in mysql database to file






















First, we need to fetch the file info from the database. Open filesLogic. Now create a file called downloads. 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. So when you click on the download link of a file, that file's id is sent to the filesLogic. 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. That's about it with file upload and download. The PHP script works on Apache web servers for all kind of files. I have used this script for file downloads even bigger than MB. The cache control header is used to force a download for text files or other files, even if they are opened by default inside your web browser.

View the code on Gist. Create on your web page links for all the file which resists in a password protected directory or in a directory above the website root directory. Use for our example the following download URL:.

With the following example, I will use a string to receive the name of a file which is stored inside a secure MySQL database.

Let say, we have a simple database table with only two columns for the ID and the filename. The code for the file download. In the first example I used the file name inside the download URL. If you really need to protect your downloads , you need to deny the direct access of the file location using Apache directives or.

Use more secure slugs to receive a file name from your database. For example you can encrypt the database row key as well. Published in: PHP Scripts. Podcast Who is building clouds for the independent developer? Exploding turkeys and how not to thaw your frozen bird: Top turkey questions Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Related Hot Network Questions.

Question feed. Stack Overflow works best with JavaScript enabled. For uploading a file we need a form for the user to enter the file name or browse their computer and select a file. Example : upload. You should not assume that the file was uploaded successfully to the server. Always check to see if the file was successfully uploaded by looking at the file size.

Our next job is to read the content of this file and insert the content to database. Always make sure that you use addslashes to escape the content.

Using addslashes to the file name is also recommended because you never know what the file name would be.



0コメント

  • 1000 / 1000