php File processing-write files and operation files
PHP file processing, there are open and close files and read the file, the file read also read a line, character, string and the entire file, then since there is read, there is definitely a write, today's article we will give you a detailed written file and operation files!
What we just said in the first three articles, "PHP file processing-open/Close Files", "PHP file Processing-read files (a character, string)" and "PHP file processing-How to read files (one line, the entire file)" has a detailed introduction, not read the small partners, you can go to see! Today we mainly explain writing file and file operations.
One: Writing data to a file
Writing data is also a common file operation in PHP, using the Fweite () and file_put_contents () functions in PHP to write data to a file.
1.fwrite () When can call also call Fputs (), their two usage is same. The fwrite () function syntax is as follows:
int fwrite (Resource $handle, string $string [, int $length])
This function writes the content string to the file pointed to by the pointer handle. If the parameter length is specified, the write is stopped after the length of the word, and if the file content is less than long, all the file contents are output.
The 2.file_put_contents () function is a new PHP5 function that has the following syntax format:
int file_put_contents (string $filename, mixed $data [, int $flags = 0 [, Resource $context]])
FileName is the file to write data to
Data is to be written
Flags can be file_use_include_path,file_append or LOCK_EX, where lock_ex are exclusive locks, which we'll cover in detail in the following articles!
Use the file_put_contents () function and call fopen (), fwrite (), and the fclose () function to implement the same functionality. Below we compare the superiority of function through concrete strength!
This example first writes data to a file using the fwrite () function, and writes the data using the File_put_contents () function, with the following example code:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $filepath =" 05.txt "; $str =" topic.alibabacloud.com www.php.cn "; echo" writes the file with the Fwrite function "; $fopen = fopen ($filepath, "WB") or Die ("file does not exist"), Fwrite ($fopen, $str), fclose ($fopen); ReadFile ($filepath); echo "<p> with file The _put_contents function writes the file: "; file_put_contents ($filepath, $STR); ReadFile ($filepath); >
The output is:
Two: Operation files
In addition to the content of PHP can read and write, the file itself can also be manipulated, such as: Copy, rename, see the date of modification and so on. PHP built-in a large number of file manipulation functions, commonly used file operation functions are the following table:
| Function prototypes |
Function description |
Example |
| BOOL Copy (string path1,string path2) |
Copy the file from path1 to path2. If successful returns TRUE, the failure returns false. |
Copy (' Tm.txt ', '. /tm.txt ') |
| BOOL Rename (string filename1,string filename2) |
Rename the name1 to Name2 |
Rename (' 1.txt ', ' test.txt ') |
| BOOL Unlink (string filename) |
Delete the file, return true successfully, and fail to return false. |
Unlink (' Tm.txt ') |
| int Fileatime (string filename) |
Returns the time the file was last accessed, returned as a Linux timestamp |
Fileatime (' Test.txt ') |
| int Filemtime (string filename) |
Returns the time the file was last modified, returned as a Linux timestamp |
Date ("y-m-d H:i:s ", Filemtime (" Test.txt ")) |
| int filesize (string filename) |
Gets the size of the file filename (bytes) |
FileSize (' 1.txt ') |
| Array PathInfo (string name[,int options]) |
Returns an array that contains the path information for the name of the file. There are dirname,basename and extension. You can set the information to be returned by option, with Pathinfo_dirname, Pathinfo_basename, and Pathinfo_extension. The default is to return all |
$arr =pathinfo ('/tm/s1/16/4/9/1.txt '); foreach ($arr as $method = $value) {
echo $method. ":". $value. " <br> ";} |
| String Realpath (String filename) |
Returns the absolute path of the file filename, such as D:\wampserver\www\test\test.txt |
Realpath (Test.txt) |
| Array stat (string filename) |
Returns an array that includes information about the file, such as the file size mentioned above, the last modification time, and so on. |
$arr =stat (' test.txt '); foreach ($arr as $method = $value) { Echo $method. ":". $value. " <br> "; } |
Attention:
When reading and writing files, in addition to file (), ReadFile () and other few functions, other errors must first use the fopen () function to open the file, and finally use the fclose () function to close the file, the information function of the file (such as: Filesize,filemtime And so on) do not need to open the file, as long as the file saved on it!
PHP file processing to the end of this, the following article we will be the PHP directory processing, specific content please read the "PHP directory processing-open/close directory"!