doudaiyao0934 2013-06-21 14:00
浏览 4
已采纳

使用PHP删除文件

I have a list of file paths that I want to delete. I placed the file paths in a plaintext file in the root directory of the server. For example:

files_to_be_removed.txt

/path/to/bad/file.php
/path/to/another/bad/file.php

In the same directory, I have another file:

remove.php

$handle = @fopen("files_to_be_removed.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        if (unlink($buffer))
            echo $buffer . ' removed.';
    }
    fclose($handle);
}

When I run my script, nothing is output. Simply, the files in the list aren’t being deleted. Why is that?

  • 写回答

2条回答 默认 最新

  • douxiong2001 2013-06-21 14:04
    关注
    $files = file('files_to_be_removed.txt', FILE_IGNORE_NEW_LINES);
    foreach ($files as $file) {
        if (@unlink($file)) {
            echo $file, ' removed', PHP_EOL;
        } else {
            $error = error_get_last();
            echo 'Couldn\'t remove ', $file, ': ', $error['message'], PHP_EOL;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改