This article summarizes an example of writing, deleting, copying, and creating and modifying php files. This article summarizes an example of writing, deleting, copying, and creating and modifying php files.
Script ec (2); script
Example 1:
Read/write files
| The Code is as follows: |
|
|
$ Filename = 'txt/write.txt ';
// Define the content to be written
$ Content = "Mr. Li 36 Shandong nr Mr. Wang 49 Hunan nr Mr. Sun 40 Hebei ";
// Use the is_writable () function to determine whether the object exists and is writable.
If (is_writable ($ filename )){
// Write the file to the end of the file. Select Operation Mark.
If (false = ($ handle = fopen ($ filename, 'A '))){
Echo "opening the file $ filename failed ";
Exit ();
}
// Write $ content to the Open File
If (fwrite ($ handle, $ content) === false ){
Echo "failed to Write File $ filename ";
Exit ();
}
Echo "the file $ filename is successfully written ";
// Close the handle
Fclose ($ handle );
} Else {
Echo "File $ filename has no write permission ";
}
?> |
Example 2 of php file writing:
| The Code is as follows: |
|
|
$ Filename = "Test \ file.txt ";
$ File = fopen ($ filename, "w"); // open the file in write mode
Fwrite ($ file, "Hello, world! N "); // write the first line
Fwrite ($ file, "This is a test! N "); // write the second row
Fclose ($ file); // close the file
?> |
Delete an object:
| The Code is as follows: |
|
|
$ Filename = "Test \ file.txt ";
Unlink ($ filename); // delete an object
?> |
Copy a file:
| The Code is as follows: |
|
|
$ Filename1 = "Test \ file.txt ";
$ Filename2 = "Test \ file. bak ";
Copy ($ filename1, $ filename2); // copy the object
?> |
File Creation, modification, and access time
Instance
| The Code is as follows: |
|
|
<? Php
$ A = filectime ("log.txt ");
Echo "creation time:". date ("Y-m-d H: I: s", $ )."
";
$ A = filemtime ("log.txt ");
Echo "modification time:". date ("Y-m-d H: I: s", $ )."
";
$ A = fileatime ("log.txt ");
Echo "Access time:". date ("Y-m-d", $ )."
";
?> |
PHP fileatime () function
Definition and usage
The fileatime () function returns the last access time of the specified object.
This function returns the time when the object was last accessed. If an error occurs, false is returned. Time is returned in Unix timestamp format.
Syntax
Fileatime (filename) filename is required. Specifies the file to be checked.
Tips and comments
Tip: the result of this function will be cached. Use clearstatcache () to clear the cache.
Note: The atime of the file should be changed whenever the data block in the file is read. Performance is affected when an application regularly accesses a large number of files or directories. Some Unix file systems can disable atime updates during loading to improve the performance of such programs. The USENET newsgroup is a common example. In this file system, this function is useless.