简体   繁体   中英

php array of pictures with redirect

There is a file with a lot location of pictures, foo.txt .

http://foo/bar1.png
http://foo/bar2.png
http://foo/bar3.png

I make an array and each is displayed, but some pictures are missing. The server thus forwards on the same 404 image for all.

http://foo/404_bar.png

How I can show pictures - if there is a redirect, that this picture does not show. We need this to understand dynamically.

$lines = file("foo.txt")
foreach ($lines as $line_num => $line) {
echo '<img src="', $line ,'"><br>';}

Browser show:

bar1.png
404_bar.png
404_bar.png

As the bar2.png and bar3.png images are missing on the server, he is displayed 404_bar.png. Me need to display only:

bar1.png

I think it is necessary to dig the headers.

as I understand from your question, you need to only show the existed images, so you should check the urls if they are exist or not:

$lines = file("foo.txt")
foreach ($lines as $line_num => $line) {
    if(is_url_exist($line))
        echo '<img src="', $line ,'"><br>';
}

function is_url_exist($url){
    $ch = curl_init($url);    
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if($code == 200){
       $status = true;
    }else{
      $status = false;
    }
    curl_close($ch);
   return $status;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM