0

I have a function which runs through an array of URL images, taken from my database, to see if they exist in order to upload images which do exist to another place. The application is in PHP using CakePhp 2.x and I'm using a function which I found on another Stackoverflow question.

The function which checks if the image exist is the next:

public function checkImageContentType($url) {
    return (@fopen($url,"r")==true);
}

But I've also test the next code:

function checkRemoteFile($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    curl_close($ch);
    if($result !== FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Its worth mentioning the images are from a remote domain so I can not use file_exist function. The code in question which runs the for of the array of url images and calls the functions to check if the images exist is the next:

foreach($inventories as $inventory) {
    if (!($this->checkImageContentType($inventory['Inventory']['imgsrc1']))) {
        echo "<p>Pass: ".$inventory['Inventory']['sku']." <a href='". $inventory['Inventory']['imgsrc1'] ."'>IMG Here</a> </p>";
        continue;
    }

    echo "<p>Product SKU:".$inventory['Inventory']['sku']."</p>";

    ....

}

If the image exists it keeps on with the function to upload, if not it continues on with the next image in array. The only problem is this code is not detecting all images for some reason. It passes on over some images which do exists and detects only a few images, this happens randomly I'm not sure why. Any ideas what I'm doing wrong or what I could try to debug this?

EDIT:

I think I found the reason, its making to many calls to the other server, so it is sending me a 403 error. Is there a way to overcome this? I'm making many calls to the other server I think that is why the other server is preventing me from making any more calls.

EDIT 2:

Just to give a little more context, the images URL where stored on Shopify Files and they have a hot link protection in order to prevent many calls from a single server. So you cant check out that many links even if the links are 404s. And there is a cooldown where Shopify wont allow you to check any more Shopify URLs, hence the 403 error. Or at least that is what I think is happening. All I had to do was keep the request to a minimum and well expand the times of my app checking for the images url from Shopify. Maybe its not the best solution, but for now it was what I found out.

If someone knows how to bypass or workaround the HotLink Protection I would like to hear the ideas :P

Jurgen Feuchter
  • 544
  • 1
  • 7
  • 29

1 Answers1

0

I assume your issue is with your checkImageContentType() method. You are using not so clever @ error suppressor which who knows what fopen($url,"r") return true/false but with suppressor will method return void?

Then you are using loose comparison using ==, you should check for identical values to be sure you are getting expected result using ===.

Also, please check if image does not exist, you don't get 404 server page response, that you might confuse as image response. Some servers might have hot linking protections.

You can always use libraries like guzzlehttp/guzzle to not re-invent the wheel, and make your life a bit easier.

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • Suppose I'm using the "checkRemoteFile" function, with that one I'm checking for each image URL using CURL but most URLs come out false I'm not even sure why. You think its the hot linking protection? The URLs in question are from Shopify Files – Jurgen Feuchter Mar 09 '19 at 03:06
  • If most are returning, then hot linking is probably not an issue. But you should check contents of those files. Test your file array by converting them into data image, and check what exactly you are seeing. – HelpNeeder Mar 09 '19 at 03:19
  • Most are returning false, even if I run 1 url which I know the image exists it returns false first. Im not even sure why anymore. – Jurgen Feuchter Mar 09 '19 at 03:21
  • Check if url structure is correct; server require https/http; your server require SSL tunel; server can send/receive files of this file; are urls encoded; please debug your image array. – HelpNeeder Mar 09 '19 at 03:36
  • The server has SSL, but still the calls are not working. – Jurgen Feuchter Mar 11 '19 at 02:59