PHP Detecting if source image url link leads to a “broken” image?

前端 未结 8 1590
迷失自我
迷失自我 2020-12-30 03:56

Suppose you have a thumbnail generator script that accepts source images in the form of a URL. Is there a way to detect if the source URL is \"broken\" - whether nonexistent

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 04:47

    try for local files
    
    
    

    for external domains

    $headers = @get_headers($url);
    if (preg_match("|200|", $headers[0])) {
    // file exists
    } else {
    // file doesn't exist
    }
    

    Also you can use curl request for the same.

提交回复
热议问题