Read File contents
PHP has a rich file operation function, the simplest function to read the file is file_get_contents, you can read the entire file into a string.
$content = file_get_contents ('./test.txt ');
The file_get_contents can also control the start point and length of the content through parameters.
$content = file_get_contents ('./test.txt ', NULL, NULL, 100, 500);
PHP also provides a method similar to the C language operation file, using methods such as Fopen,fgets,fread, fgets can read a line from the file pointer, Freads can read a string of a specified length.
$fp = fopen ('./text.txt ', ' RB '), while (!feof ($fp)) { echo fgets ($FP);//reads a row of}fclose ($FP);
$fp = fopen ('./text.txt ', ' RB '); $contents = "; while (!feof ($fp)) { $contents. = Fread ($fp, 4096);//read 4,096 characters at a time} Fclose ($FP);
Using the fopen open file, it is best to use fclose to close the file pointer to avoid the file handle being occupied.
Determine if a file exists
In general, in the operation of the file needs to determine the existence of the file, PHP commonly used to determine the existence of a file function has two is_file and file_exists.
$filename = './test.txt '; if (file_exists ($filename)) { echo file_get_contents ($filename);}
If you just judge the existence of the file, use file_exists on the line, file_exists can not only determine whether the file exists, but also can determine whether the directory exists, from the function name can be seen, Is_file is the exact decision to determine whether a given path is a file.
$filename = './test.txt '; if (Is_file ($filename)) { echo file_get_contents ($filename);}
It is more accurate to use is_readable and is_writeable to determine whether a file is readable or writable, based on whether the file exists.
$filename = './test.txt '; if (is_writeable ($filename)) { file_put_contents ($filename, ' Test ');} if (is_readable ($filename)) { echo file_get_contents ($filename);}
Get the file modification time
The file has many meta attributes, including: The owner of the file, creation time, modification time, last access time, and so on.
Fileowner: Get the file owner filectime: Get File creation time Filemtime: Get File modification time Fileatime: Get file access time
One of the most commonly used is the file modification time, through the file modification time, you can judge the timeliness of the file, often used in static files or cached data updates.
$mtime = Filemtime ($filename); Echo ' Modified time: '. Date (' y-m-d h:i:s ', Filemtime ($filename));
Get the size of the file
The size of the file can be obtained through the FileSize function, and the file size is expressed as a number of bytes.
$filename = '/data/webroot/usercode/resource/test.txt '; $size = FileSize ($filename);
If you want to convert the units of a file size, you can define your own function to implement it.
function GetSize ($size, $format = ' kb ') { $p = 0; if ($format = = ' kb ') { $p = 1; } elseif ($format = = ' mb ') { $p = 2; } elseif ($format = = ' GB ') { $p = 3; } $size/= Pow (1024x768, $p); Return Number_format ($size, 3);} $filename = '/data/webroot/usercode/code/resource/test.txt '; $size = FileSize ($filename); $size = GetSize ($size, ' KB '); Perform unit conversions Echo $size. ' KB ';
It is important to note that there is no simple function to get the size of the directory, the size of the directory is the sum of all subdirectories and the size of the file, so the recursive way to calculate the size of the directory.
16/7/11_php-Read File contents