[英]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
我的脚本:
opendir();
创建的数组检查丢失的数据库文件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。
以下是一些需要考虑或尝试的事项:
hash
,然后使用fetch_all
到一个数值数组中。scandir
在目录中构建文件数组array_diff
删除$fichiers_a_exclures
和$images
sha1
测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.