How can one check to see if a remote file exists using PHP?

前端 未结 22 3031
死守一世寂寞
死守一世寂寞 2020-11-22 05:52

The best I could find, an if fclose fopen type thing, makes the page load really slowly.

Basically what I\'m trying to do is

22条回答
  •  梦谈多话
    2020-11-22 06:39

    This works for me to check if a remote file exist in PHP:

    $url = 'https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico';
        $header_response = get_headers($url, 1);
    
        if ( strpos( $header_response[0], "404" ) !== false ) {
            echo 'File does NOT exist';
            } else {
            echo 'File exists';
            }
    

提交回复
热议问题