PHP reads files and supports code sharing for remote files _ PHP Tutorial

Source: Internet
Author: User
PHP reads files and supports code sharing of remote files. Php file reading case 1 copy the code as follows :? Php $ filejb51.net. php; this case does not support remote $ fsofopen ($ file, r); echo $ datafread ($ fso, filesize ($ file); fcl php reads files

Case 1

The code is as follows:


$ File = 'jb51. net. php ';
// Remote connection is not supported in this case.
$ Fso = fopen ($ file, 'r ');
Echo $ data = fread ($ fso, filesize ($ file ));
Fclose ($ fso );
?>


Fopen () binds the name resource specified by file to a stream.
Filesize: the number of bytes of the file size. If an error occurs, FALSE is returned.
Note: Because the integer type of PHP is signed, and most platforms use 32-bit integers, the filesize () function may return unexpected results when encountering a file larger than 2 GB. for files between 2 GB and 4 GB, sprintf ("% u", filesize ($ file) can be used to overcome this problem.
Fread () reads a maximum of length bytes from the file pointer handle. this function reads the length of several bytes, or when it reaches the EOF, or (for network streams) when a package is available, it stops reading files, depending on the first situation.
Note: Usage of earlier versions! We recommend that you use file_get_contents for php5.

Case 2

The code is as follows:


$ File = 'jb51. net. php ';
// Remote support
$ File = 'http: // www.jb51.net ';//
Echo $ data = implode ('', file ($ file ));
?>


File-read the entire file into an array
Description
Read binary files

Case 3

The code is as follows:


$ File = 'http: // www.jb51.net ';
Echo file_get_contents ($ file );
?>


File_get_contents -- read the entire file into a string
Description
String file_get_contents (string filename [, int use_include_path [, resource context])
Like file (), only file_get_contents () is returned as a string.
The file_get_contents () function is the preferred method to read the file content into a string. if the operating system supports this function, the memory ing technology is used to improve the performance.

The code of the explain case is as follows :? Php $ file = 'jb51. net. php'; // remote $ fso = fopen ($ file, 'r'); echo $ data = fread ($ fso, filesize ($ file) is not supported in this case )); fcl...

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.