0

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

表:图像

行:哈希

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

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

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

图像名称示例:d0a0bb3149bea2335e8784812fef706ad0a13156.jpg

我的脚本:

  1. 我选择数据库中的图像
  2. 我把它放在一个数组中
  3. 我使数组随机(以避免总是检查前 35,000 张图像)
  4. 我在文件夹/images中创建了一组图像文件
  5. 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。

4

1 回答 1

1

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

  • 将 '.jpg' 连接到hashsql 中,然后用于fetch_all数值数组。
  • 用于scandir在目录中构建文件数组
  • 用于array_diff删除$fichiers_a_exclures$images
  • 遍历这个最小的数组来做sha1测试
于 2020-01-18T12:32:32.163 回答