Several Methods for PHP to read files

Source: Internet
Author: User
Tags alphanumeric characters
I sorted out several methods for reading files in PHP for future reference. 1. freadstringfread (int $ handle, int $ length) fread () reads a maximum of length bytes from the file pointed to by handle. This function can be read by a maximum of length bytes, or reaches the EOF, or (for network streams) when a package is available

I sorted out several methods for reading files in PHP for future reference. 1. fread string fread (int $ handle, int $ length) fread () reads a maximum of length bytes from the file pointed to by the handle. This function can be read by a maximum of length bytes, or reaches the EOF, or (for network streams) when a package is available

I sorted out several methods for reading files in PHP for future reference.

1. fread

  String fread (int $ handle, int $ length)

Fread () reads a maximum of length bytes from the file pointed to by handle. This function can be read by a maximum of length bytes, or when it reaches the EOF, or (for network streams) when a package is available, or (after opening the user space Stream) when 8192 bytes have been read, the system stops reading files.

Fread () returns the read string. If an error occurs, FALSE is returned.

 

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:

 

Or:

 

2. fgets

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

 

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

  String fgetss (resource $ handle [, int $ length [, string $ allowable_tags])

Similar to the fgets function, 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.

 ');    }    fclose($handle);?>

4. file

  Array file (string $ filename [, int $ use_include_path [, resource $ context])
Read the file content into an array. Each item in the array corresponds to a row in the file, including a line break. You can use the rtrim () function to filter line breaks when no line terminator is required.

  $content){        echo 'line '.($line + 1).':'.$content;    }?>

5. readfile

  Int readfile (string $ filename [, bool $ use_include_path [, resource $ context])

Read a file and write 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.

 

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. Third Parameter$ ContextIt can be used to set some parameters, such as setting timeout when accessing a remote file.

In addition, compared with the above functions, file_get_contents has a much better performance. Therefore, file_get_contents should be preferred. But readfile seems to have better performance than file_get_contents(?)Because it does not need to call fopen.

 Array ('timeout' => 1 // set timeout); echo file_get_contents ("http://www.baidu.com/", 0, $ ctx);?>

7. fpassthru

  Int fpassthru (resource $ handle)

Read the given file pointer from the current location to EOF and write the result to the output buffer.

 

8. parse_ini_file

  Array parse_ini_file (string $ filename [, bool $ process_ps])

Parse_ini_file () loads an ini file specified by filename and returns the settings as a Union array. If the final process_ps parameter is set to TRUE, a multidimensional array is obtained, including the name and settings of each section in the configuration file. The default value of process_ps is FALSE.

Note:

1. If the value in the ini file contains any non-alphanumeric characters, enclose it in double quotation marks (").
2. Some Reserved Words cannot be used as the key names in the ini file, including: null, yes, no, true, and false. The values are null, no and false are equivalent to "", and yes and true are equivalent to "1 ". Character {} | &~! [() "Cannot be used anywhere in the key name, and these characters have special meanings in the option value.

Content of the test. ini file:

; This is a sample configuration file; Comments start with ';', as in php.ini[first_p]one = 1five = 5animal = BIRD[second_p]path = "/usr/local/bin"URL = "http://www.example.com/~username

8. parse_ini_file

  Array parse_ini_file (string $ filename [, bool $ process_ps])

Parse_ini_file () loads an ini file specified by filename and returns the settings as a Union array. If the final process_ps parameter is set to TRUE, a multidimensional array is obtained, including the name and settings of each section in the configuration file. The default value of process_ps is FALSE.

Note:

1. If the value in the ini file contains any non-alphanumeric characters, enclose it in double quotation marks (").
2. Some Reserved Words cannot be used as the key names in the ini file, including: null, yes, no, true, and false. The values are null, no and false are equivalent to "", and yes and true are equivalent to "1 ". Character {} | &~! [() "Cannot be used anywhere in the key name, and these characters have special meanings in the option value.

Content of the test. ini file:

; This is a sample configuration file; Comments start with ';', as in php.ini[first_p]one = 1five = 5animal = BIRD[second_p]path = "/usr/local/bin"URL = "http://www.example.com/~username

Test. php content:

     print_r($config);?>

Several notes:

 

1. We encourage you to use it when processing binary files.BMark, even if the system does not need it, this can make the script more portable.

 

2. The allow_url_fopen option activates the URL-based fopen encapsulation protocol to allow access to URL objects such as files. The default Encapsulation Protocol provides ftp and http protocols to access remote files. 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 (such as spaces), you need to use urlencode () for URL encoding.

 

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.