I like to remove images that are not used from the server
The code i write is working but is very slow take more than 30 min. is there any fatser way to do it ?
Files: around 150k
DB records: around 120k
Query executed time: 1 sec.
$sql = "SELECT image, media FROM products p, product_media pm WHERE p.id = pm.id";
$result = mysqli_query($con, $sql) or die(mysql_error());
$row = mysqli_fetch_all ($result, MYSQLI_ASSOC);
$directory = "images/products/full";
$images = glob("$directory/*.{jpg,jpeg,png,bmp}", GLOB_BRACE);
foreach($images as $key => $image) {
$path = $image;
$image = str_replace('images/products/full/', '', $image);
if (!in_array($image, $row)) {
unlink($path);
}
}