PHP reads files and supports code sharing for remote files

  • 2020-05-26 07:53:21
  • OfStack

php reads the file

Case 1
 
<?php 
$file = 'ofstack.com.php'; 
// Remote 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 one stream.
filesize returns the number of bytes in the file size and FALSE in the case of an error.
Note: because the integer type of PHP is signed and most platforms use 32-bit integers, the filesize() function may return unexpected results when it encounters a file larger than 2GB. sprintf("%u", filesize($file)) can usually be used for files between 2GB and 4GB to overcome this problem.
fread() reads a maximum of length bytes from the file pointer handle. This function stops reading the file after reading length bytes, or when it reaches EOF, or (in the case of network streams) when a packet is available, whichever occurs first.
Description: low version usage! file_get_contents is recommended for php5

Case 2
 
<?php 
$file = 'ofstack.com.php'; 
// Supports remote  
$file = 'https://www.ofstack.com';// 
echo $data = implode('', file($file)); 
?> 

file - reads the entire file into an array
instructions
Read files in base 2

Case 3
 
<?php 
$file = 'https://www.ofstack.com'; 
echo file_get_contents($file); 
?> 

file_get_contents -- reads the entire file into a single string
instructions
string file_get_contents ( string filename [, int use_include_path [, resource context]])
Like file() 1, only 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 single string. Memory mapping techniques are also used to improve performance if supported by the operating system.

Related articles: