I’m simply trying to delete a file using PHP’s unlink. I found an answer on here that told me the best way to do this and I implemented it. Heres the code I am using.
$log_path = realpath($log_file);
if (is_readable($log_path)){
print file_exists($log_path);
$deleted = unlink($log_path);
print "Deleted:".$deleted;
print "Exists:".file_exists($log_path);
}
This just prints 1Deleted:exists:1. I also tried to add in print_r(error_get_last()); under the unlink, but this also returned nothing. All files in the directory are chmod 777 * and there are no other file handlers open. ….what the heck…
The code you used is needlessly cumbersome. A simple call to unlink should do the trick:
But let’s find out what is going wrong. The file exists, because you enter the loop where the print statements are done. The
unlinkcall probably returns false, because the output is “11” and not “111”.So my gut says, it must be a file permissions issue. Are you sure the web user has permission to remove the file? Can you show us the folder permissions, for instance by running
ls -laon the command line and pasting the output?