About PHP fread () Tips for using _php tips

Source: Internet
Author: User
Tags fread
Description
string fread (int handle, int length)
Fread () reads up to length bytes from the file pointer handle. The function stops reading a file when it reads up to a maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or (after opening a user-space stream), reads 8,192 bytes, depending on which situation you first encounter.
Returns a string that is read, FALSE if an error is returned.
Copy Code code as follows:

<?php
Get contents's a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen ($filename, "R");
$contents = Fread ($handle, FileSize ($filename));
Fclose ($handle);
?>

Warning
When you open a file on a system that distinguishes binaries and text files (such as Windows), the mode parameter of the fopen () function is added ' B '.
Copy Code code as follows:

<?php
$filename = "C:\\files\\somepic.gif";
$handle = fopen ($filename, "RB");
$contents = Fread ($handle, FileSize ($filename));
Fclose ($handle);
?>

Warning
When read from any not normal local file, such as when reading a stream returned from a remote file or Popen () and Proc_open (), the read stops after a package is available. This means that the data should be collected and merged into chunks as shown in the following example.
Copy Code code as follows:

<?php
For PHP 5 and later
$handle = fopen ("http://www.example.com/", "RB");
$contents = Stream_get_contents ($handle);
Fclose ($handle);
?>

<?php
$handle = fopen ("http://www.example.com/", "RB");
$contents = "";
while (!feof ($handle)) {
$contents. = Fread ($handle, 8192);
}
Fclose ($handle);
?>

Note: If you just want to read the contents of a file into a string, with file_get_contents (), it is much better than the code above.
Extra:
File_get_contents
(PHP 4 >= 4.3.0, PHP 5)
File_get_contents-Reads the entire file into a string
Description
String file_get_contents (string filename [, bool Use_include_path [, resource context [, int offset [, int maxlen]]]

As with file (), only file_get_contents () reads the file into a string. Reads the content of length MaxLen at the location specified by the parameter offset. If it fails, file_get_contents () returns FALSE.
The file_get_contents () function is the preferred method for reading the contents of a file into a string. Memory-mapping technology is also used to enhance performance if supported by the operating system.

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.