Php allow file downloads






















I know I can just remove the file right afterwards, but is there any simpler way? Yes the file needs to exist on the server before readfile can work correctly, what you can do is use a cron job so that every 10 minutes or so it deletes files created with a time stamp of more then 10 minutes earlier.

So for example. File time stamp : Server time stamp : One last question. Is there any way possible that you can use the same active PHP script that creates the file and offers it to the user, to delete the file about a minute later? 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. Output The following output will appear after clicking the download link of the image file.

What you must do — always — is sanitize the input. Accept only file names, like this:. Anything is better than blindly accept requests. If you need to restrict access to a file, you should generate encrypted, one-time IDs, so you can be sure a generated path can be used only once. This is a very widespread problem and unfortunately even the PHP manual is plagued with errors.

There is no such thing in HTTP. You may add those headers if you want, but they do absolutely nothing. Sadly, this wrong example is present even in the PHP manual. The author must have been really frustrated and added three Content-Type headers. What would it be like to not having to worry about old versions of Internet Explorer? Note: the quotes in the filename are required in case the file may contain spaces.

The code above will fail in IE6 unless the following are added:. Now, the use of Cache-Control is wrong in this case, especially to both values set to zero, according to Microsoft , but it works in IE6 and IE7 and later ignores it so no harm done.

If you still get strange results when downloading especially in IE , make sure that the PHP output compression is disabled, as well as any server compression sometimes the server inadvertently applies compression on the output produced by the PHP script.

Historically it had some performance issues and while the documentation claims there are no memory problems, real-life scenarios beg to differ — output buffering and other subtle things. Regardless, if you need byte ranges support, you still have to output the old-fashioned way. You just output a header and the module takes care of the rest. This is the source of many seemingly obscure errors.



0コメント

  • 1000 / 1000