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

是否有一个脚本或其他任何我可以运行的文件,并将搜索没有链接到的文件?

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

有帮助吗?

解决方案

试试这个,只是不要忘记改变你的目录 $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

其他提示

如果在更新链接后没有概率再次需要这些文件,并且您没有与它们有多个链接的文件,我建议您在更新链接时删除这些文件。即:

  1. Link1 指向 File1
  2. 更新资料 Link1 指向 File2
  3. 删除 File1 马上。

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

您可以使用许多免费链接检查器工具。在运行它的网站(过滤图片/ PDF文件)后,您可以采取生成的列表并以编程方式检查其图像/ PDF目录,以查找列表中的内容。请记住,这可能难以确定性地确定为动态生成的世代生成的世纪(基于用户输入/设置,Apache Rewrites,通过代码返回的文件)可能不包含。

如果它是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命令中,您将找到一个允许您在很长时间删除尚未访问/修改/编辑的文件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top