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

眉间皱痕 提交于 2021-02-05 11:17:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!