I put an Android APK installation package on the server side (Apache environment) Download link, using the readfile()
read apk file output download, the phone installation apk display parsing package error. But the APK itself is not a problem, the size of the downloaded file is also complete. The server MIME is also set. Then fread()
the output is good after swapping. Don't understand why. Searched for a bit. It is clear that there is no explanation, because it is said to be "binary friendly".
including, and file_get_contents()
so on, fgets()
several other file read functions and methods, has not been very good to distinguish when to use which. In addition to manuals, are there any good introductory articles with application scenarios?
Fill in the code before and after:
ReadFile ():
header("Content-type: application/vnd.android.package-archive"); header("Accept-Ranges: bytes");header("Accept-Length:".$filesize ); header("Content-Disposition: attachment; filename=".$name);readfile($file);
Fread ():
header("Content-type: application/vnd.android.package-archive"); header("Accept-Ranges: bytes");header("Accept-Length:".$filesize ); header("Content-Disposition: attachment; filename=".$name);$handle = fopen($file,"r"); echo fread($handle,$filesize);fclose($handle);
If it is directly written or read all directly with file_get_contents () file_put_contents () more convenient
Fread can read the specified size, read the specified line and so on, but also can lock the file more powerful
The explanations in the ReadFile manual are as follows:
The ReadFile () function outputs a file.
The function reads in a file and writes to the output buffer.
If successful, returns the number of bytes read from the file if it fails, and returns false, which outputs the number of bytes at the end of the document.
fread()
Andreadfile()
fread()
Maximum one performance reads 8k bytes in length, so you cannot read large files at once to download them. The advantage is that the operation is more flexible and the content of the specified bytes is read each time to facilitate the control of the server's traffic when downloading.
readfile()
The advantage is the ability to read large files at once, without the need for PHP pre-read to memory, download faster, directly to the processing of files to the server. The disadvantage is that the load cannot be controlled. So it is no memory limit, if you encounter a memory error, call ob_end_flush()
a function such as the first to close the buffer.
file_get_contents
is also not readfile()
fast, because also walked the memory of PHP. But when reading small text content to a string variable, this function is best used, simple, and faster.
fgets()
is to read one line of the file at a time, and then you need to process the file line by row, use this
PHP output file, how to distinguish when to use ReadFile (), Fread (), file_get_contents (), fgets ()