Summary of common methods for reading files in PHP

Source: Internet
Author: User
Tags fread readfile
    1. $filename = "/usr/local/something.txt";
    2. $handle = fopen ($filename, "R");//The second parameter needs to be set to ' RB ' when reading a binary file
    3. Get the file size by FileSize and read the entire file into a string
    4. $contents = Fread ($handle, FileSize ($filename));
    5. Fclose ($handle);
    6. ?>
Copy Code

You cannot use this method if the file you are reading is not a local normal file, but a remote file or a stream file, because FileSize cannot get the size of these files. At this point, you need to determine whether the end of the file has been read by the return value of feof () or fread ().

For example:

    1. $handle = fopen (' http://bbs.it-home.org ', ' R ');
    2. $content = ";
    3. while (!feof ($handle)) {
    4. $content. = Fread ($handle, 8080);
    5. }
    6. Echo $content;
    7. Fclose ($handle);
    8. ?>
Copy Code

Or:

    1. $handle = fopen (' http://bbs.it-home.org ', ' R ');
    2. $content = ";
    3. while (false! = ($a = fread ($handle, 8080))) {//returns false to indicate that the end of the file has been read
    4. $content. = $a;
    5. }
    6. Echo $content;
    7. Fclose ($handle);
    8. ?>
Copy Code

2. Fgets method string fgets (int $handle [, int $length]) fgets () reads a line from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.

    1. $handle = fopen ('./file.txt ', ' R ');
    2. while (!feof ($handle)) {
    3. Echo fgets ($handle, 1024);
    4. }
    5. Fclose ($handle);
    6. ?>
Copy Code

Description: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are larger than 8KB, specifying the length of the largest row in the script is more efficient with resources. Starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not.

3. The Fgetss method string Fgetss (Resource $handle [, int $length [, String $allowable _tags]] is the same as fgets, but FGETSS attempts to remove from the text read any HTML and PHP tags, you can use the optional third parameter to specify which tags are not removed.

    1. $handle = fopen ('./file.txt ', ' R ');
    2. while (!feof ($handle)) {
    3. Echo fgetss ($handle, 1024, '
      ');
    4. }
    5. Fclose ($handle);
    6. ?>
Copy Code

4. Filearray file (string $filename [, int $use _include_path [, Resource $context]]) reads the contents of the files into an array, each item of the array corresponds to a line in the file, including the newline character. You can use the RTrim () function to filter line breaks when you don't need a line terminator.

    1. $a = File ('./file.txt ');
    2. foreach ($a as $line = = $content) {
    3. Echo ' line '. ($line + 1). ': ' $content;
    4. }
    5. ?>
Copy Code

5. ReadFile method int ReadFile (string $filename [, bool $use _include_path [, Resource $context]]) reads into a file and writes to the output buffer. Returns the number of bytes read from the file. If an error returns false and the error message is displayed unless it is called in the form of @readfile ().

    1. $size = ReadFile ('./file.txt ');
    2. Echo $size;
    3. ?>
Copy Code

1 2 Next last page

  • Related Article

    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.