Several common functions for php to read files-PHP source code

Source: Internet
Author: User
Ec (2); several common functions used in the php tutorial to read files: The file_get_contents: file_get_contents () function reads the entire file into a string. Like file (), the difference is that file_get_contents () reads the file into a string. The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to add script ec (2); script

Several common functions for reading files in the php tutorial

File_get_contents:

The file_get_contents () function reads the entire file into a string.

Like file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.

Syntax
File_get_contents (path, include_path, context, start, max_length

$ Url = "http://www.111cn.net ";
$ Contents = file_get_contents ($ url );
// Add the following code if Chinese characters are garbled
// $ Getcontent = iconv ("gb2312", "UTF-8", file_get_contents ($ url ));
// Echo $ getcontent;
Echo $ contents;
?>


2. curl:

If you see this, you need to set your php and enable this library. If you are on windows, it is very simple. You need to modify the settings of your php. ini file, find php_curl.dll, and cancel the semicolon comment, as shown below:
// Cancel the comment
Extension = php_curl.dll

$ Url = "http://www.111cn.net ";
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, curlopt_url, $ url );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_connecttimeout, $ timeout );
// Add the following two lines to the webpage for User Detection
// Curl_setopt ($ ch, curlopt_httpauth, curlauth_any );
// Curl_setopt ($ ch, curlopt_userpwd, us_name. ":". us_pwd );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ contents;
?>


3. fopen-> fread-> fclose:

Detailed descriptions of fopen Functions

Definition and usage
The fopen () function opens a file or url.

If opening fails, this function returns false.

Syntax
Fopen (filename, mode, include_path, context) parameter description
Filename is required. Specifies the file or url to open.
Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below.
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or true.
Context is optional. Rules


Open the file in read-only mode and point the file pointer to the file header.
Open the "r +" read/write mode and point the file pointer to the file header.
Open the "w" Write mode, point the file pointer to the file header, and cut the file size to zero. Create.
Open in "w +" read/write mode, point the file pointer to the file header, and cut the file size to zero. Create.
"A" is opened in writing mode, pointing the file pointer to the end of the file. The file does not exist.
Open the "a +" read/write mode and point the file pointer to the end of the file. The file does not exist.

For more details, see: http://www.111cn.net/phper/php-function/35709.htm

$ Handle = fopen ("http://www.111cn.net", "rb ");
$ Contents = "";
Do {
$ Data = fread ($ handle, 8192 );
If (strlen ($ data) = 0 ){
Break;
}
$ Contents. = $ data;
}
While (true );
Fclose ($ handle );
Echo $ contents;
?>

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.