Handling file download






















On some systems, there can be issues with the usual file saving process that prevent you from doing it the easiest way. However, we can use another technique that works - most of the time ;-. This technique is only necessary when we don't have a direct file link, which is usually the case when the file being downloaded is based on a more complicated data export.

There are actually 2 techniques to do this so if one would not work for you, you can try the other one :. We use the mysterious.

Basically, it extends Puppeteer's functionality. Let's wait for one minute. In real use-case, you want to check the state of the file in the file system. And extract the file from the file system into memory. We have to first find its name and then we can read it. To do this, in essence, we can trigger the file download, intercept the request going out and then replicate it to get the actual data.

First we need to enable request interception. This is done using the following line of code:. More info on this in Puppeteer docs. 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.

If you have output buffering, the file will not be sent to the user in chunks but only at the end of the script. At the start of your script, after checking the file if it exists, etc. Be aware that multiple ranges can be specified e.

If the range is not valid, you must output. Do not try to guess or fix the range s as it may result in corrupted downloads, which are more dangerous than failed ones. Many developers forget to send the code or the Accept-Ranges. Yet others forget that when you send a range, the Content-Length must match the length of the range rather than the size of the whole file. You can output the file using the method described above, skipping until the start of the range and delivering the length of the range.

I did my best to provide only accurate information.



0コメント

  • 1000 / 1000