How to download a single file using php script






















The solution to this problem is to download the file forcibly using the built-in PHP readfile function. The readfile function is used in PHP script to forcibly download any file of the current location, or the file with the file path. The syntax of this function is given below. This function can take three arguments. The first argument is mandatory, and the other two arguments are optional. This function returns the number of bytes read from the file mentioned in the first argument.

The uses of this function are shown in the following two examples. In this example, we will create an HTML file with the following code, where the file name will be passed as a parameter of the URL named path , and the value of this parameter will be passed to the PHP file named download.

We will create the PHP file with the following code to download the file forcibly. Next, the header function is used to set the necessary header information before using the readfile function. The basename function is used to retrieve the filename, and the filesize function is used to read the size of the file in bytes, which will be shown in the opening dialog box to download the file.

The flush function is used to clear the output buffer. The readfile function is used with the filename only, here. 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.

Most JavaScript examples…. I ditched Apache so many years ago… I thought it was practically shipped with nowadays. Just another reason to switch to an nginx-based solution. I think lighty has supported it for a long time too. Then they should move away from shared hosts.

Sure they should move away, but many of them stick with shared hosting because of the higher costs for managed server hosting. Hi, i used your code to create a download. I would like to protect also my folder from url access, if i use as chmod value, the page will not return the download file, but it will stay blank the fopen fail.. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default.

You can force images or other kind of files to download directly to the user's hard drive using the PHP readfile function. Here we're going to create a simple image gallery that allows users to download the image files from the browser with a single mouse click. If you see the above example code carefully, you'll find the download link pints to a "download.



0コメント

  • 1000 / 1000