check if image exists php

前端 未结 3 1330
清酒与你
清酒与你 2021-01-11 12:24

I am in the middle of coding up a property portal. I am stuck on checking images. I know how to check if an image url is set. But the problem is detecting if there is actual

相关标签:
3条回答
  • 2021-01-11 12:57

    Use getimagesize() to ensure that the URL points to a valid image.

    if (getimagesize($imageURL) !== false) {
        // display image
    }
    
    0 讨论(0)
  • 2021-01-11 12:59
    function is_webUrl($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);
        if (curl_exec($ch) !== FALSE) {
            return true;
        } else {
            return false;
        }
    }
    
    if(is_webUrl('http://www.themes.tatwerat.com/wp/ah-personal/wp-content/uploads/2016/08/features-ah-wp-view.jpg')) {
       echo 'yes i found it';
    }else{
       echo 'file not found';
    }
    
    0 讨论(0)
  • 2021-01-11 13:07
    function exists($uri)
    {
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        return $code == 200;
    }
    
    0 讨论(0)
提交回复
热议问题