Page 1 of 1

Copy images from remote url to images folder

Posted: Sat Nov 12, 2022 5:40 pm
by tessthepup
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 :ugeek:

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;

Re: Copy images from remote url to images folder

Posted: Sat Nov 12, 2022 7:25 pm
by ecartz
Have you tried removing the @ signs? It may be that they are suppressing error messages that would tell you what was happening.

Re: Copy images from remote url to images folder

Posted: Sat Nov 12, 2022 7:29 pm
by tessthepup
ecartz wrote: Sat Nov 12, 2022 7:25 pm Have you tried removing the @ signs? It may be that they are suppressing error messages that would tell you what was happening.
@ecartz Yes tried with the @ removed and the images still copy but no error messages on screen or in the error log

Re: Copy images from remote url to images folder

Posted: Sun Nov 13, 2022 12:07 am
by ecartz
Perhaps change

Code: Select all

   if (!@copy($url, $dest)) {
to

Code: Select all

copy($url, $dest);
clearstatcache();
if (!file_exists($dest)) {
Because the behavior that you're getting suggests that copy is returning the wrong result. So instead of using its result, use file_exists which seems to be working. And yes, I realize that this will result in you using file_exists twice on the same path.

You may or may not need clearstatcache. It's even possible that just adding the clearstatcache() call after the previous file_exists and before the copy would work without any other changes.

See https://stackoverflow.com/a/6930271/6660678

Re: Copy images from remote url to images folder

Posted: Sun Nov 13, 2022 9:43 am
by tessthepup
ecartz wrote: Sun Nov 13, 2022 12:07 am Perhaps change

Code: Select all

   if (!@copy($url, $dest)) {
to

Code: Select all

copy($url, $dest);
clearstatcache();
if (!file_exists($dest)) {
Because the behavior that you're getting suggests that copy is returning the wrong result. So instead of using its result, use file_exists which seems to be working. And yes, I realize that this will result in you using file_exists twice on the same path.

You may or may not need clearstatcache. It's even possible that just adding the clearstatcache() call after the previous file_exists and before the copy would work without any other changes.

See https://stackoverflow.com/a/6930271/6660678
@ecartz
I think I have sorted it by doing this

Code: Select all

if (!file_exists($dest)) {   //prevent file overwriting
   if (copy($url, $dest)) {
In the end all I did was remove the !@ from copy but leave the ! on file_exists :roll:

I will monitor it, thanks for all the help :D