'Downloading 1000s of images using php , how to bypass blank images

I have a php file that is creating a list of url paths of images i want to download. Currently i have just been creating a list , copying and pasting it in a desktop app that will download images by url list in txt file. I wanted to go ahead and add the download to the php file to bypass that current step.

currently the following generates a list of urls i use to copy and paste to desktop downloader app

foreach ($espn_ar as $key => $value) echo 'ht'.'tps://a.espncdn.com/i/headshots/nfl/players/full/'.$espn_ar[$key].'.png<br>';

the list that is generated looks like this , with approximately 2500 url paths

https://a.espncdn.com/i/headshots/nfl/players/full/2580.png
https://a.espncdn.com/i/headshots/nfl/players/full/2330.png
https://a.espncdn.com/i/headshots/nfl/players/full/2977742.png
https://a.espncdn.com/i/headshots/nfl/players/full/5528.png
https://a.espncdn.com/i/headshots/nfl/players/full/5529.png
https://a.espncdn.com/i/headshots/nfl/players/full/5536.png
https://a.espncdn.com/i/headshots/nfl/players/full/5713.png
https://a.espncdn.com/i/headshots/nfl/players/full/8439.png
https://a.espncdn.com/i/headshots/nfl/players/full/8461.png
https://a.espncdn.com/i/headshots/nfl/players/full/8479.png

To bypass the step of copying and pasting to a desktop app , i added the following

foreach ($espn_ar as $key => $value) {
    $url = 'ht' . 'tps://a.espncdn.com/i/headshots/nfl/players/full/' . $espn_ar[$key] . '.png';
    $img = 'images/' . $espn_ar[$key] . '.png';
    $ch = curl_init($url);
    $fp = fopen($img, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}

This seems to be working to download all images into a directory setup called "images"

Question 1 - is what i have done the best method for what i'm trying to achieve ?

Question 2 - when using the desktop app it doesn't download a blank image if the url path doesn't contain an image, however using the php method it does download a blank image if one of the url paths doesn't contain any image, so how can i add something to not download a blank png file, or only download files over 1kb, or delete all files 1kb or smaller after all files downloaded, not sure what is best way to handle that.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source