我有我的文件夹/images(有 ~ 95.000 个文件),我检查每个文件是否在数据库中。
表:图像
行:哈希
该文件夹包含我所有带有 sha1 名称的图像。
我shuffle($images);用来确保验证是随机的,否则它只验证前 35,000 张图像。
如果我检查超过 35,000 次,脚本会设置超时并且页面会阻止它。
图像名称示例:d0a0bb3149bea2335e8784812fef706ad0a13156.jpg
我的脚本:
- 我选择数据库中的图像
- 我把它放在一个数组中
- 我使数组随机(以避免总是检查前 35,000 张图像)
- 我在文件夹/images中创建了一组图像文件
opendir();我使用函数创建的数组检查丢失的数据库文件- 我显示答案
<?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。