删除与列表不匹配的文件

时间:2013-09-17 20:09:26

标签: php cron

所以我想创建一个简单的脚本,它将有一个预定义文件列表,搜索列表中没有的任何内容并删除它。

我现在有这个

<?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文件并检查而不是数据库?

谢谢!

2 个答案:

答案 0 :(得分:2)

您错过了两次尾随/ ..在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>";

?>

答案 1 :(得分:1)

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

$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);
    }
}
相关问题