Home » Php » php – Download remote file using laravel

php – Download remote file using laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’ve a link that gets the image, but the image has been removed, renamed or moved to another place

public function index()
{    
    $array = [
         { image:'https://imgimob.sobressai.com.br/fotos/765/475139/12768822_g.jpg' },
         { image:'http://imgimob.sobressai.com.br/fotos/765/475140/12768824_g.jpg' }
    ]    
    for ($i = 0; $i < count($array); $i++) {
       if(!$contents = file_get_contents($array[$i]['image'])) 
       {
             return 'Not found!';
       else {
        Storage::disk('public')->put('default/teste.jpg', $contents); 
       } 
   }
}

if the image no exist then I get this error

file_get_contents(http://imgimob.sobressai.com.br/fotos/765/475140/12768824_g.jpg): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I would link get to return “doesnt’ exist” and download file if it does.

How to&Answers:

You’re not handling the warning properly. You can’t just use ! in your if / else statement.

(I’m assuming you’re using laravel with the methods above.)

You devise some sort of function / method for it. You could do it like so:

private function checkImage($url)
{
    $valid_types = ['image/png', 'image/jpeg']; // add more valid types that you want and so on
    $client = new \GuzzleHttp\Client();
    try {
        $response = $client->head($url);
        return
            !empty($response->getHeaders()['Content-Type'][0]) &&
            in_array($response->getHeaders()['Content-Type'][0], $valid_types, true);

    } catch (\Exception $e) {
        return false;
    }
}

Then finally just use the method in your class:

$url = 'https://imgimob.sobressai.com.br/fotos/765/475140/12768824_g.jpg';
$exists = $this->checkImage($url);
echo $exists ? 'yay!' : 'does not exist'; // or do the echo inside the function whichever you prefer

I used the ->head method above to just check and not include the body.

Edit:

For the purposes of actually donwloading the file. Just use the same code above, along with your Storage saving method to save the image. Using ->get():

$url = 'https://imgimob.sobressai.com.br/fotos/765/475139/12768822_g.jpg';
$valid_types = ['image/png', 'image/jpeg']; // add more valid types that you want and so on
$client = new \GuzzleHttp\Client();
try {
    $response = $client->get($url);
    if (
        !empty($response->getHeaders()['content-type'][0]) &&
        in_array($response->getHeaders()['content-type'][0], $valid_types, true)
    ) {
        Storage::disk('public')->put('default/teste.jpg', $response->getBody()->getContents());
    }

} catch (\Exception $e) {
    echo 'does not exist';
}

Answer:

If you are certain that the file does exist, then this is a permission issue. Check the file’s owner and mode are the ones you expect. (edited)

Also, make sure that your PHP is configured with allow-url-fopen enabled. This will authorize PHP to open remote files. Read more at https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen