开发者

Get a 0KB file after using PHP unlink() method

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 "开发者_如何学JAVAFailed to delete.";
}

But after I run the code, the file still exists and becomes 0KB. Anyone knows how to get around this?


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";
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜