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

前端 未结 22 2910
死守一世寂寞
死守一世寂寞 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:28

    If you're using the Symfony framework, there is also a much simpler way using the HttpClientInterface:

    private function remoteFileExists(string $url, HttpClientInterface $client): bool {
        $response = $client->request(
            'GET',
            $url //e.g. http://example.com/file.txt
        );
    
        return $response->getStatusCode() == 200;
    }
    

    The docs for the HttpClient are also very good and maybe worth looking into if you need a more specific approach: https://symfony.com/doc/current/http_client.html

提交回复
热议问题