What are some alternative functions or methods in PHP that can be used to check the modification time of files before deleting them?
When deleting files in PHP, it's important to check the modification time of the file to ensure that you are not accidentally deleting a file that has been recently modified. One way to do this is by using the filemtime() function to get the last modification time of the file before deciding to delete it.
$filename = 'example.txt';
// Get the last modification time of the file
$lastModifiedTime = filemtime($filename);
// Check if the file was modified within the last 24 hours
if (($lastModifiedTime + 86400) > time()) {
// File was modified within the last 24 hours, do not delete
echo "File was modified within the last 24 hours, cannot delete.";
} else {
// File was not modified within the last 24 hours, safe to delete
unlink($filename);
echo "File deleted successfully.";
}
Keywords
Related Questions
- How can the choice of text editor impact the handling of character encoding in PHP scripts and data files?
- What are the potential benefits and drawbacks of using URL redirects in .htaccess to mimic subdomains in PHP?
- How can error reporting settings impact the functionality of preg_match when working with $_COOKIE in PHP?