Read File contents
PHP has a wealth of file manipulation functions. The simplest function to read a file is file_get_contents, which is able to read all the entire file into a string.
$content = file_get_contents ('./test.txt ');
The file_get_contents is also able to read the start point and length of the content by using the parameter control.
$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 row from the file pointer, freads be able to 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.
Infer whether a file exists
In general, in the operation of the file need to infer the existence of the file, PHP often used to infer 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);}
Assuming that only the inferred file exists, use file_exists. File_exists not only can infer whether a file exists, but also can infer the existence of the folder at the same time, from the function name can be seen. Is_file is the exact inference of whether a given path is a file.
$filename = './test.txt '; if (Is_file ($filename)) { echo file_get_contents ($filename);}
More precise can use is_readable and is_writeable to infer 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);}
Write content to File
Corresponding to the read file. PHP also has two ways to write files, the simplest way is to use file_put_contents.
$filename = './test.txt '; $data = ' Test '; file_put_contents ($filename, $data);
In the example above, the $data can be a one-dimensional array, and when $data is an array, it will voluntarily concatenate the array, equivalent to$data=implode(‘‘, $data);
Similarly, PHP supports a C language style of operation, using fwrite for file writing.
$fp = fopen ('./test.txt ', ' W '); fwrite ($fp, ' hello '); fwrite ($fp, ' World '); fclose ($FP);
Get the file change time
The file has a very diverse set of attributes, including: All of the files, creation time, change time, last access time, etc.
Fileowner: Get all the files filectime: Get the file creation time filemtime: Get the file change time fileatime: Get the file's access time
The most commonly used is the file change time, through the file change time. Be able to infer the timeliness of a file, often using updates to static files or cached data.
$mtime = Filemtime ($filename); Echo ' Change 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. The size of the file is expressed as a number of bytes .
$filename = '/data/webroot/usercode/code/resource/test.txt '; $size = FileSize ($filename);
Suppose you want to convert a file size unit. Ability to customize functions to implement.
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 ');//Unit conversions echo $size. ' KB ';
It is important to note that there is no simple function to get the size of the folder. The size of the folder is the sum of all the subfolders and the size of the file under the folder, so the size of the folder needs to be recursively computed.
deleting files
Similar to UNIX system commands, PHP uses the unlink function for file deletion.
Unlink ($filename);
Delete a directory using the RmDir function, the directory must be empty, assuming that it is not empty or that there is no permission to prompt for failure.
RmDir ($dir);
Assume that a file exists in the folder. The ability to cycle through all files in a folder is first. Then delete the folder. Circular deletion can use the Glob function to traverse all files.
foreach (Glob ("*") as $filename) { unlink ($filename);}
PHP File System