Copy images from remote url to images folder
Posted: Sat Nov 12, 2022 5:40 pm
Hi Guys,
I have wrote a script that copies product images from a suppliers website by cross referencing the image name in the database and appending it to the url for copy and then saving the images to the catalog image folder.
I did have an issue saving the image but that is sorted now and the images copy successfully, the only issue I have now is with the way the reporting works.
The code below should report the following
1. If the image is new then show the image name with link and a badge that says 'new'
2. If the image already exists show the image name with link and a badge that says 'image already exists' *this does work when checking if image exists*
3. If there is a problem saving the image then show the image name with link and a badge that says 'ERROR saving image'
The problem lies in the logic where it does actually copy the images to the server but skips step one and reports 'ERROR saving image' instead of 'new'
Also the count does not work anymore even though it did before I resolved the problem of copying the images to the folder.
Any ideas as it has me puzzled
I have wrote a script that copies product images from a suppliers website by cross referencing the image name in the database and appending it to the url for copy and then saving the images to the catalog image folder.
I did have an issue saving the image but that is sorted now and the images copy successfully, the only issue I have now is with the way the reporting works.
The code below should report the following
1. If the image is new then show the image name with link and a badge that says 'new'
2. If the image already exists show the image name with link and a badge that says 'image already exists' *this does work when checking if image exists*
3. If there is a problem saving the image then show the image name with link and a badge that says 'ERROR saving image'
The problem lies in the logic where it does actually copy the images to the server but skips step one and reports 'ERROR saving image' instead of 'new'
Also the count does not work anymore even though it did before I resolved the problem of copying the images to the folder.
Any ideas as it has me puzzled
Code: Select all
if (!@file_exists($dest)) { //prevent file overwriting
if (!@copy($url, $dest)) {
//report successful image saved
$notify.= '<div class="card text-center mx-auto mt-2" style="width: 28rem;">
<ul class="list-group list-group-flush">
<li class="list-group-item">' . $row['products_name'] . ' - <a href="' . $supplier_url . ''. $row['products_image'] . '" target="_blank" rel="noopener noreferrer">'. $row['products_image'] . '</a> <span class="badge badge-success">New</span></li>
</ul>
</div>';
$count++;
} else {
//report error saving image
$notify.= '<div class="card text-center mx-auto mt-2" style="width: 28rem;">
<ul class="list-group list-group-flush">
<li class="list-group-item">' . $row['products_name'] . ' - <a href="' . $supplier_url . ''. $row['products_image'] . '" target="_blank" rel="noopener noreferrer">'. $row['products_image'] . '</a> <span class="badge badge-danger">ERROR saving image</span></li>
</ul>
</div>';
}
} else {
//report image already exists
$notify.= '<div class="card text-center mx-auto mt-2" style="width: 28rem;">
<ul class="list-group list-group-flush">
<li class="list-group-item">' . $row['products_name'] . ' - <a href="' . $supplier_url . ''. $row['products_image'] . '" target="_blank" rel="noopener noreferrer">'. $row['products_image'] . '</a> <span class="badge badge-warning">image already exists</span></li>
</ul>
</div>';
}
}
//output result
echo '<div class="col-5 text-center mx-auto mt-4"><div class="alert alert-success" role="alert">Total Images Saved: <b>' .$count .'</b></div></div>';
echo $notify;