douqiao6015 2018-10-15 11:45
浏览 50
已采纳

在项目列表中删除php中的文件

I have a php script to display if a list of computers are active or not. Every minute or so it sends a packet to my server and it updates a file.

The php script I made checks if the file has been updated within the last 90 seconds. If it has, it will display it as active, else it will be displayed as inactive.

$thelist = "";
if ($handle = opendir('ips/')) {
    while (false !== ($file = readdir($handle))) {
        if (($file != ".") && ($file != "..") && (substr($file, - 4) == ".ips")) {
            $active = true;
            $activetext = "<a class=activeGreen>● Active</a> ";
            if(humanTiming(filemtime("ips/".$file)) > 90) {
                $activetext = "<a class=inactiveRed>● Inactive</a> ";
                $active = false;
            }
            if ($active == true) {
                $thelist = $activetext.'<a>'.basename($file, ".ips")."</a> <a>[Delete]</a><br>".$thelist;
            } else {
                $thelist .= $activetext.'<a>'.basename($file, ".ips")."</a> <a>[Delete]</a><br>";
            }
        }
    }
    closedir($handle);

    echo "<ul><p>$thelist</p></ul>";
}

function humanTiming($time) {
    $time = time() - $time; // to get the time since that moment
    $time = $time < 1 ? 1 : $time;
    $tokens = [1 => 'second'];

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits;
    }
}

This all works, but how would I be able to make the delete button next to them delete that specific file when it's clicked?

  • 写回答

1条回答 默认 最新

  • dongti8535 2018-10-27 07:41
    关注

    What I ended up doing was to create a second php file, accepting url get arguments.

    <?php
        if(!empty($_GET["fname"])) {
            if(file_exists($_GET["fname"])) {
                echo 'Deleting ' . htmlspecialchars($_GET["fname"]) . '!';
                unlink($_GET["fname"]);
            } else {
                echo 'Error: ' . htmlspecialchars($_GET["fname"]) . ' does not exist';
            }
        } else {
            echo 'Direct access not allowed';
        }
    ?>
    

    PHP Url Arguments

    PHP Delete File

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?