Example of php writing, deleting, copying, and creating and modifying files

Source: Internet
Author: User

Example 1:

Read/write files

The code is as follows: Copy code

<? Php
$ 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: Copy code

<? Php
$ 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: Copy code

<? Php
$ Filename = "Test \ file.txt ";
Unlink ($ filename); // delete an object
?>

Copy a file:

The code is as follows: Copy code

<? Php
$ 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: Copy code

<? 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.