Common methods for reading files in PHP-php Tutorial

Source: Internet
Author: User
Summary of common methods for reading files in PHP

  1. $ Filename = "/usr/local/something.txt ";
  2. $ Handle = fopen ($ filename, "r"); // when reading binary files, set the second parameter to 'RB'
  3. // Obtain the file size through filesize and read the entire file to a string
  4. $ Contents = fread ($ handle, filesize ($ filename ));
  5. Fclose ($ handle );
  6. ?>

If the file to be read is not a local common file, but a remote file or stream file, this method cannot be used, because filesize cannot obtain the size of these files. In this case, you need to use the returned values of feof () or fread () to determine whether the object has been read to the end.

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. ?>

Or:

  1. $ Handle = fopen ('http: // bbs.it-home.org ', 'r ');
  2. $ Content = '';
  3. While (false! = ($ A = fread ($ handle, 8080) {// Return false to indicate that the object has been read to the end.
  4. $ Content. = $;
  5. }
  6. Echo $ content;
  7. Fclose ($ handle );
  8. ?>

2. fgets method string fgets (int $ handle [, int $ length]) fgets () reads a row from the file pointed to by handle and returns a string with a maximum length of 1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value 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. ?>

Note: The length parameter is optional since PHP 4.2.0. if ignored, the row length is assumed to be 1024. Starting from PHP 4.3, ignoring length will continue to read data from the stream until the row ends. If most of the rows in the file are larger than 8 kB, it is more effective to specify the length of the maximum row in the script to use resources. Starting from PHP 4.3, this function can be safely used for binary files. Earlier versions do not work.

3. the fgetss method string fgetss (resource $ handle [, int $ length [, string $ allowable_tags]) has the same function as fgets, however, fgetss will try to remove any HTML and PHP tags from the read text. you can use the optional third parameter to specify which tags will not be removed.

  1. $ Handle = fopen ('./file.txt', 'r ');
  2. While (! Feof ($ handle )){
  3. Echo fgetss ($ handle, 1024 ,'
    ');
  4. }
  5. Fclose ($ handle );
  6. ?>

4. filearray file (string $ filename [, int $ use_include_path [, resource $ context]) reads the file content into an array, each of which corresponds to a row in the file, including line breaks. You can use the rtrim () function to filter line breaks when no line terminator is required.

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

5. readfile method int readfile (string $ filename [, bool $ use_include_path [, resource $ context]) reads a file and writes it to the output buffer. Returns the number of bytes read from the file. If an error is returned, FALSE is returned, and the error message is displayed unless called in the form of @ readfile.

  1. $ Size = readfile ('./file.txt ');
  2. Echo $ size;
  3. ?>

12. Next page

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.