开发者

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

开发者 https://www.devze.com 2023-01-22 00:17 出处:网络
I\'m trying to delete a file on the server. Below is the code I use. function ServerDel($file){ $file = realpath($file);

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

精彩评论

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