This article mainly for you in detail the php file download instance code, with a certain reference value, interested in small partners can refer to
The function of file download is basically necessary for a website, so let's take a look at how PHP implements the file download today.
No control type
The type of uncontrolled type referred to here is those types of resources that can be downloaded directly without adding PHP code control. Generally, compressed files. rar; Audio and video files:. Avi and so on can also be downloaded directly. However, the audio and MP4 will be directly parsed by the browser.
Such as:
. avi file
. rar Files
. Mp4,mp3, pictures, etc. will be parsed directly
Core code
Using PHP code to control the download of the file is very simple. We live with very little code to do such a complex job.
Type
Use the basename function to get the name of the file instead of the path information, protecting the server's directory security header ("Content-disposition:attachment;filename=". BaseName ($ filename));
Length
Header ("Content-length:". FileSize ($filename));
Implementation functions
<?php$filename = $_get[' filename '];//uses the basename function to obtain the name of the file instead of the path information, protecting the server's directory security header ("Content-disposition: Attachment;filename= "$filename); Header (" Content-length: ". FileSize ($filename)); ReadFile ($filename); >
Optimization
We didn't use a function called basename when we specified the file name, so that when we downloaded the file, we would have the full path to the file on the server.
Note: The name of the downloaded file must not contain its full path name on the server, or it may cause a partial resource leak. Even though there are ways to control access to resources, it is good to use the basename function.
The name of the original download file
Optimized file Download name
Summarize
PHP file download is really too convenient to use compared to Java implementation. Within the download file, there is a lot of information provided to us, through these packaged information, we can achieve a more elegant file download system.
Summary: The above is the entire content of this article, I hope to be able to help you learn.