-1

How I can check if file exists on the url with laravel?

I try:

if (is_file('http://example.com/badsass323.txt')) {
   //do something
}

But not working.. condition is not return true

Dumitru
  • 2,053
  • 6
  • 20
  • 45

1 Answers1

0

Laravel comes with GuzzleHttp, so you can use it to send a request to any URL. After that you can check the type got in the response using getHeader().

Example:

//load the client
$client = new \GuzzleHttp\Client();
try {
    //send request
    $request = $client->request('GET', 'https://www.google.com.br/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');

    //Check content type received
    if($request->getHeader('content-type')[0] == 'image/png'){
        echo "It's a PNG image";
    } else {

    }
} catch(Exception $e) {
    //handle it
}