问题
I'm trying to delete a file on the server. Below is the code I use.
function ServerDel($file){
$file = realpath($file);
echo ($file);
$fh = fopen($file, 'w') or die("can't open file");
fclose($fh);
if(unlink($file))
echo"Delete the file successfully.";
else
echo "Failed to delete.";
}
But after I run the code, the file still exists and becomes 0KB. Anyone knows how to get around this?
回答1:
use a
flag in fopen()
instead of w
.
$fh = fopen($file, 'a') or die("can't open file");
Try this:
function ServerDel($file){
$rfile = realpath($file);
echo ($rfile);
if (file_exists($rfile)) {
if(unlink($rfile)) {
echo "Delete the file successfully.";
} else {
echo "Failed to delete.";
}
} else {
echo "File does not exist";
}
}
来源:https://stackoverflow.com/questions/4011727/get-a-0kb-file-after-using-php-unlink-method