PHP output file, how to distinguish when to use ReadFile (), Fread (), file_get_contents (), fgets ()

Source: Internet
Author: User
Tags fread ranges readfile

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.

      1. 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.
      2. file_get_contentsis 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.
      3. 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.