繁体   English   中英

如果文件夹中的文件在数据库中,如何优化我的脚本来扫描文件夹中的文件?

[英]How do I optimize my script that scans files in a folder if they are in the database?

我有我的文件夹/images (有 ~ 95.000 个文件),我检查每个文件是否在数据库中。

表:图像

行:哈希

该文件夹包含我所有带有 sha1 名称的图像。

我使用shuffle($images); 确保验证是随机的,否则只验证前 35,000 张图像。

如果我检查超过 35,000 次,脚本会设置超时并且页面会阻止它。

图像名称示例: d0a0bb3149bea2335e8784812fef706ad0a13156.jpg

我的脚本:

  1. 我选择数据库中的图像
  2. 我把它放在一个数组中
  3. 我使数组随机(以避免总是检查前 35,000 张图像)
  4. 我在文件夹/images 中创建了一组图像文件
  5. 我使用opendir();创建的数组检查丢失的数据库文件opendir(); 功能
  6. 我显示答案
<?php
set_time_limit(0);

$images = [];
$q = $mysqli->query('SELECT hash FROM images');
while($r = $q->fetch_assoc())
{
    $images[] = $r['hash'].'.jpg';
}

shuffle($images);

$i_hors_bdd = 0;
$images_existent_hors_bdd = [];

if($dh = opendir($_SERVER['DOCUMENT_ROOT'].'/images'))
{
    while(($file = readdir($dh)) !== false)
    {
        if(!in_array($file, $fichiers_a_exclures))
        {
            if(!is_sha1($file) OR !in_array($file, $images))
                $images_existent_hors_bdd[] = '<p><a href="?del='.$file.'">Name of File: '.$file.'</a></p>';
        }

        if($i_hors_bdd > 35000)
        {
            break;
        }

        $i_hors_bdd++;
    }
}

closedir($dh);


if(count($images_existent_hors_bdd) > 0)
{
    echo '<p>Image exist, but not in the databse.</p>';

    sort($images_existent_hors_bdd);

    foreach($images_existent_hors_bdd as $image_existe_hors_bdd)
        echo $image_existe_hors_bdd;
}

else
    echo '<p>All images are in datase.</p>';

echo '<p>'.$i_hors_bdd.' images checked.</p>';

所以我的问题是:如何优化此脚本以提高脚本的速度以允许检查更多图像而不阻塞脚本? 知道我的VPS不是很强大而且我没有SSD。

以下是一些需要考虑或尝试的事项:

  • 将 '.jpg' 连接到 sql 中进行hash ,然后使用fetch_all到一个数值数组中。
  • 使用scandir在目录中构建文件数组
  • 使用array_diff删除$fichiers_a_exclures$images
  • 迭代这个最小的数组来做sha1测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM