Several methods of reading file contents in b.php

Source: Internet
Author: User
Tags fread readfile

Several ways to read file contents in PHP 1.fread

  string fread (int $handle, int $length)

Fread () reads up to length bytes from a file pointed to by handle. The function stops reading the file when it has read up to the maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or when 8,192 bytes have been read (after opening a stream of user space), depending on which situation is encountered first.

Fread () returns the read string if an error returns FALSE.

<?php$filename = "/usr/local/something.txt";$handle =fopen ( $filename," R "); // when reading binary files , you need to set the second parameter to ' RB '//Get the file size through FileSize, and read the entire file into a string   $contents = fread (  $handle, filesize ($ filename)); fclose ( $handle);?            


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:

<?php$handle =fopen (' http://www.baidu.com ', ' R ');$content = "; While (!  Feof ($handle)) { $content. = fread ($handle, 8080);} echo $content; fclose ($handle);? >          

Or:

<?php$handle =fopen (' http://www.baidu.com ', ' R ');$content = ";Whilefalse! = ( $a = fread ( $handle, 8080)) {//  returns false indicating that the end of the file has been read   $content. =  $a;} echo  $content; Span style= "Color:rgb (0, 128, 128); line-height:1.5!important ">fclose ( $handle);?             

2.fgets

 String fgets (int $handle [, int $length])

Fgets () reads a row from the file pointed to by handle and returns a string 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.

& lt;? PHP  $handle = fopen ('./file.txt ', ' R '); while (!feof ( $handle)) { Span style= "Color:rgb (0, 0, 255); line-height:1.5!important ">echo fgets ( $handle, 1024); } fclose ( $handle);?           

  Note: 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.fgetss

  String Fgetss (Resource $handle [, int $length [, String $allowable _tags]])

As with the Fgets function, but FGETSS tries to remove any HTML and PHP tags from the text that is being read, you can specify which tags are not removed with the optional third parameter.

<?php$handle = fopen ('./file.txt ', ' R '); While (!  Feof ($handle)) { echo fgetss ($handle, 1024x768, ' <br> ');} fclose ($handle);? >         

4.file

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

& lt;? PHP  $a = file ('./file.txt '); foreach ( $a as  $line =  $content) {echo ' line '. ( $line + 1). ': '  $content; }?>           

5.readfile

  int ReadFile (string $filename [, bool $use _include_path [, Resource $context]])

Reads 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 ().

<?php    ReadFile ('./file.txt ');    $size;? > 

6.file_get_contents

  String file_get_contents (String $filename [, bool $use _include_path [, Resource $context [, int $offset [, int $maxlen] ]]] )

Reads a file into a string. The third parameter $context can be used to set parameters such as setting timeouts when accessing remote files, and so on.

In addition, File_get_contents has a much better performance than several of these functions, so it should be preferable to use file_get_contents. But ReadFile looks better than file_get_contents (? ), because it does not need to call fopen.

& lt;? PHP  $ctx = stream_context_create (  array (' http ' = = array (' timeout ' = 1 // set timeout echo file_get_contents (" http://www.baidu.com/", 0,  $ctx);           
7.fpassthru

int Fpassthru (resource $handle)

Reads the given file pointer from the current location to EOF and writes the result to the output buffer.

& lt;? PHP header ("content-type:text/html;charset=     Utf-8 ");  $handle = fopen ('./test2.php ', ' R '); fseek ( $handle, 1024); // position the pointer to 1024 bytes Office   Fpassthru ( $handle);?           

Several caveats:

1. Encourage the use of the B flag when working with binary files, even if the system does not need it, to make the script more portable.

2. The allow_url_fopen option activates the URL form of the fopen encapsulation protocol so that URL objects such as files can be accessed. The default encapsulation protocol provides access to remote files with FTP and HTTP protocols, and some extension libraries such as zlib may register more encapsulation protocols. For security reasons, this option can only be set in php.ini.

3. If you want to open a URL with special characters (for example, a space), you need to use UrlEncode () for URL encoding.

Source: http://www.cnblogs.com/gyrgyr/p/5774436.html

Several methods of reading file contents in b.php

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.