Home > Backend Development > PHP Tutorial > Examples of PHP getting (reading) the contents of local files and remote files

Examples of PHP getting (reading) the contents of local files and remote files

WBOY
Release: 2016-07-25 08:54:28
Original
1201 people have browsed it
  1. $file = 'jbxue.com.php';
  2. //Remote is not supported
  3. $fso = fopen($file, 'r');
  4. echo $data = fread($fso , filesize($file));
  5. fclose($fso);
  6. ?>
Copy code

fopen() Binds the name resource specified by file to a stream. filesize returns the file size in bytes, or FALSE if an error occurs. Note: Because PHP's integer types are signed, and most platforms use 32-bit integers, the filesize() function may return unexpected results when encountering files larger than 2GB. For files between 2GB and 4GB, it is usually You can use sprintf("%u", filesize($file)) to overcome this problem. fread() reads up to length bytes from the file pointer handle. This function stops reading the file after reading length bytes, or when EOF is reached, or (for network streams) when a packet is available. , it depends on which situation is encountered first. Note: Low version usage! It is recommended to use file_get_contents for php5

Example 2,

  1. $file = 'jbxue.com.php';
  2. //Support remote
  3. $file = 'http://bbs.it-home.org';//
  4. echo $ data = implode('', file($file));
  5. ?>
Copy code

file -- Read the entire file into an array illustrate Read binary files

Example 3,

  1. $file = 'http://bbs.it-home.org';
  2. echo file_get_contents($file);
  3. ?>
Copy code

file_get_contents - - Read the entire file into a string illustrate string file_get_contents ( string filename [, int use_include_path [, resource context]]) Same as file(), except file_get_contents() returns the file as a string. The file_get_contents() function is the preferred method for reading the contents of a file into a string. If the operating system supports it, memory mapping technology will also be used to enhance performance.

The above has shared three examples of PHP code to obtain remote files. I hope it will be helpful to everyone.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template