What are some potential pitfalls of deleting files that are currently in use for downloads or open in a browser?
Deleting files that are currently in use for downloads or open in a browser can lead to errors or unexpected behavior for the user. To prevent this, you can first check if the file is in use before attempting to delete it. This can be done by using the `flock()` function in PHP to lock the file and prevent other processes from accessing it while you delete it.
$file_path = 'path/to/file';
$handle = fopen($file_path, 'r');
if (flock($handle, LOCK_EX)) {
unlink($file_path);
flock($handle, LOCK_UN);
} else {
// File is in use, handle error or notify user
}
fclose($handle);