Answer
is what i have done the best method for what i'm trying to achieve ?
Answers to such kind of questions are generally based on your explanation of 'best'. Since you said you have very little knowledge of php, I just guess you want a simple approach, and the simplest approach I can find is:
file_put_contents($img, file_get_contents($url));
Notice that you should set allow_url_fopen = On
and enable openssl (extension=openssl
) in php.ini
.
Here is a full example:
foreach ($espn_ar as $value) {
$url = "https://a.espncdn.com/i/headshots/nfl/players/full/$value.png";
$img = "images/$value.png";
$content = file_get_contents($url);
file_put_contents($img, $content);
}
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 downlaoded, not sure what is best way to handle that.
Because I can't check all the URLs, I guess these blank images may be caused by 404 error, or the images are really blank. I'll check them all.
$content = file_get_contents($url);
# download failed
if($content === false)
continue;
# 404 error
if(strpos($http_response_header[0], '404 Not Found') !== false)
continue;
# size < 1kb
if(strlen($content) < 1024)
continue;
file_put_contents($img, $content);
0 comments:
Post a Comment
Thanks