使用PHP unlink()方法后获取0KB文件 [英] Get a 0KB file after using PHP unlink() method

查看:69
本文介绍了使用PHP unlink()方法后获取0KB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除服务器上的文件.下面是我使用的代码.

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

但是运行代码后,该文件仍然存在,并且变为0KB.有谁知道如何解决这个问题?

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

推荐答案

fopen()中使用 a 标志而不是 w .

$fh = fopen($file, 'a') or die("can't open file");

尝试一下:

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

这篇关于使用PHP unlink()方法后获取0KB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆