1

我正在一个网站上工作,其中包含许多定期更新的图像和 PDF 文件,但在上传新文件后旧文件不会被删除。因此,我有很多文件只是坐在服务器上而没有被使用。

有没有我可以运行的脚本或其他任何东西,并且会搜索没有任何链接的文件?

编辑:
我不是在问将来如何上传新文件并删除旧文件。我已经处理好了。
我只想知道如何摆脱不再使用的文件。
那有意义吗?

4

4 回答 4

2

试试这个,只是不要忘记改变你的目录$dir = "/Your/directory/here";

<?
$findex = array();
$findex[path] = array();
$findex[file] = array();

$extensions = array('.cfm','.html','.htm','.css','.php','.gif','.jpg','.png','.jpeg','.dwt');
$excludes = array('.svn');

function rec_scandir($dir)
        {
        $files = array();
        global $findex;
        global $extensions;
        global $excludes;

        if ( $handle = opendir($dir) ) 
        {
        while ( ($file = readdir($handle)) !== false ) 
            {
            if ( $file != ".." && $file != "." ) 
                {
                if ( is_dir($dir . "/" . $file) ) 
                        {
                        $files[$file] = rec_scandir($dir . "/" . $file);
                        }
                else 
                        {
                        for ($i=0;$i<sizeof($extensions);$i++)
                            {
                            if (strpos(strtolower($file),strtolower($extensions[$i])) > 0)
                                {
                                $found = true;
                                }
                            }
                        for ($i=0;$i<sizeof($excludes);$i++)
                            { 
                            if (strpos(strtolower($file),strtolower($excludes[$i])) > 0)
                                {
                                $found = false;
                                }
                            }
                        if ($found)
                            {
                            $files[] = $file;
                            $dirlink = $dir . "/" . $file;
                            array_push($findex[path],$dirlink);
                            array_push($findex[file],$file);
                            }
                        $found = false;
                        }
                    }
                }
            closedir($handle);
            return $findex;
            }
        }


$dir = "/Your/directory/here";

echo "\n";
echo " Searching ". $dir ." for matching files\n";

$files = rec_scandir($dir);

echo " Found " . sizeof($files[file]) . " matching extensions\n";

echo " Scanning for orphaned files....\n";

$findex[found] = array();

for ($i=0;$i<sizeof($findex[path]);$i++)
        {
        echo $i . " ";
        $contents = file_get_contents($findex[path][$i]);
        for ($j=0;$j<sizeof($findex[file]);$j++)
                {
                if (strpos($contents,$findex[file][$j]) > 0)
                        {
                        $findex[found][$j] = 1;
                        }
                }
        }

echo "\n";

$counter=1;
for ($i=0;$i<sizeof($findex[path]);$i++)
        {
        if ($findex[found][$i] != 1)
                {
                echo  " " . $counter . ") " .  substr($findex[path][$i],0,1000) . " is orphaned\n";
                $counter++;
                }
        }

?>

资料来源:http ://sun3.org/archives/297

于 2012-01-25T20:43:24.173 回答
1

您可以使用许多免费的链接检查器工具。在您的网站上运行它(过滤图像/pdf文件)之后,您可以获取该生成的列表并以编程方式将其与您的图像/pdf目录进行检查,以找出列表中没有的内容。请记住,这可能很难确定,因为src/href可能不包括动态生成的(基于用户输入/设置、apache 重写、通过代码返回的文件)。

于 2012-01-25T20:44:24.643 回答
1

如果在更新链接后您不可能再次需要这些文件并且您没有包含多个链接的文件,我建议您在更新链接时删除这些文件。IE:

  1. Link1指着File1
  2. 更新Link1指向File2
  3. 立即删除File1

如果在您的场景中,您可能有多个链接指向同一文件或可能在短时间内重新链接的文件,我建议设置一个 cron 作业,该作业将每周执行一次示例,并将检查您files/目录中的所有文件links如果没有引用该特定文件的链接,请删除数据库中的表。

于 2012-01-25T20:49:26.607 回答
1

如果它是 unix 服务器,请使用以下find命令:

find /tmp/web_tmp \( \( \( -type f -amin +120 \) -or \( -type f -amin +30 -size 20480k \) \) -exec rm {} \; \) -or \( -depth -type d -empty -exec rmdir {} \; \)

在这种情况下,我正在 /tmp/web_tmp 文件夹中查找空文件夹以及 120 分钟内未访问或 30 分钟内未访问且超过 20Mb 的文件。一旦创建,它将删除找到的条目

也许在find命令中你会发现一些东西可以让你删除很长时间没有被访问/修改/编辑的文件。

于 2012-01-25T21:32:28.430 回答