删除与列表不匹配的文件


Delete files not matching a list

所以我正在尝试制作一个简单的脚本,它将有一个预定义文件的列表,搜索列表中没有的任何内容并将其删除。

我现在有这个

<?php
$directory = "/home/user/public_html";
$files = glob($directory . "*.*");
foreach($files as $file)
{
    $sql = mysql_query("SELECT id FROM files WHERE FileName='$file'");
    if(mysql_num_rows($sql) == 0)
        unlink($directory . $file);
}
?>

但是,我想避免查询,以便我可以更频繁地运行脚本(大约有 60-70 个文件,我想每 20 秒左右运行一次?)那么我将如何将文件列表嵌入到 php 文件中并检查它而不是数据库?

谢谢!

您两次缺少尾随/glob()你把/home/user/public_html*.*作为论据,我认为你的意思是/home/user/public_html/*.*.

这就是为什么我打赌没有什么与您表中的文件匹配。这也不会给出错误,因为语法很好。

然后你unlink()的地方再做一次......你的论点home/user/public_htmltestfile.html应该home/user/public_html/testfile.html

我喜欢这种语法风格:"{$directory}/{$file}"因为它简短且更具可读性。如果缺少 /,您会立即看到它。您也可以将其更改为 $directory . "/" . $file ,您喜欢它。一行条件语句也是如此。所以它来了..

<?php
$directory = "/home/user/public_html";
$files = glob("{$directory}/*.*");
foreach($files as $file)
{
    $sql = mysql_query("SELECT id FROM files WHERE FileName='"{$file}'";");
    if(mysql_num_rows($sql) == 0)
    {
        unlink("{$directory}/{$file}");
    }
}
?>

编辑:您请求递归。来了..

你需要创建一个函数,你可以用路径作为参数运行一次。然后,您可以从该函数内部对子目录运行该函数。喜欢这个:

<?php
/*
    ListDir list files under directories recursively
    Arguments: 
    $dir        = directory to be scanned
    $recursive  = in how many levels of recursion do you want to search? (0 for none), default: -1 (for "unlimited")
*/
function ListDir($dir, $recursive=-1)
{
    // if recursive == -1 do "unlimited" but that's no good on a live server! so let's say 999 is enough..
    $recursive = ($recursive == -1 ? 999 : $recursive);
    // array to hold return value
    $retval = array();
    // remove trailing / if it is there and then add it, to make sure there is always just 1 / 
    $dir = rtrim($dir,"/") . "/*";
    // read the directory contents and process each node
    foreach(glob($dir) as $node) 
    {
        // skip hidden files
        if(substr($node,-1) == ".") continue;
        // if $node is a dir and recursive is greater than 0 (meaning not at the last level or disabled)
        if(is_dir($node) && $recursive > 0) 
        {
            // substract 1 of recursive for ever recursion.
            $recursive--;
            // run this same function again on itself, merging the return values with the return array
            $retval = array_merge($retval, ListDir($node, $recursive));
        } 
        // if $node is a file, we add it to the array that will be returned from this function
        elseif(is_file($node)) 
        {
          $retval[] = $node;
          // NOTE: if you want you can do some action here in your case you can unlink($node) if it matches your requirements..
        }
    }
    return $retval;
}
// Output the result
echo "<pre>";
print_r(ListDir("/path/to/dir/",1));
echo "</pre>";
?>

如果列表不是动态的,请将其存储在数组中:

$myFiles = array (
    'some.ext',
    'next.ext',
    'more.ext'
);
$directory = "/home/user/public_html/";
$files = glob($directory . "*.*");
foreach($files as $file)
{
    if (!in_array($file, $myFiles)) {
       unlink($directory . $file);
    }
}