Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcelofabio's avatar

download a file from url

Hi guys,

How can i download a file from url .zip and copy in laravel storage?

I need download a zip file to laravel storage and unzip it.

Thanks for all

0 likes
12 replies
bashy's avatar

What about Guzzle get and then save it to where ever you want while unzipping with some sort of package/command?

1 like
marcelofabio's avatar

Hi Joe,

Can i put file using laravel storage class using file_put_contents after?

Tks.

1 like
marcelofabio's avatar

Joe,

When i use file_get_contents() this is returned:

failed to open stream: Redirection limit reached, aborting

I try use cURL but return empty

1 like
mehany's avatar

Curl proved to be faster for some reason, when I tried GuzzleHttp for url grabbing with parameters I ran into issues like you so i used curl . can you try this:

    $ch = curl_init ( $url );
    curl_setopt( $ch , CURLOPT_HEADER, 0);
    curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);
    $raw=curl_exec( $ch );
    curl_close ( $ch );

    // $raw has the file, do what ever you want with it.

If you run into time out issues, try to increase the value a little bit in the right places.

Edit: Curl proved to be faster over file_get_content( )

1 like
marcelofabio's avatar

Hi mehane,

After i apply your sugestion this is returned in verbose curl:

* Hostname was found in DNS cache
* Hostname in DNS cache was stale, zapped
*   Trying 200.201.171.210...
* Connected to www1.caixa.gov.br (200.201.171.210) port 80 (#510)
> GET /loterias/_arquivos/loterias/D_megase.zip HTTP/1.1
Host: www1.caixa.gov.br
Accept: */*

< HTTP/1.1 302 Found
< Location: http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip
< Set-Cookie: security=true; path=/
< Connection: close
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< 
* Closing connection 510
* Issue another request to this URL: 'http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip'
* Hostname was found in DNS cache
*   Trying 200.201.171.210...
* Connected to www1.caixa.gov.br (200.201.171.210) port 80 (#511)
> GET /loterias/_arquivos/loterias/D_megase.zip HTTP/1.1
Host: www1.caixa.gov.br
Accept: */*

The Content-Length: 0.

1 like
bashy's avatar
bashy
Best Answer
Level 65

Looks like they have protection on their server for crawlers.

1 like
Val's avatar

Assume you want an HTML for google home page to get saved locally:

$contents = file_get_contents('www.google.com');
Storage::disk('local')->put('google.html', $contents);

Look up Laravel Storage facade documentation for details on disk configuration and put method.

alfrednutile's avatar

Yes many years later I know :) just wanted to share

The one key thing here for me is being able to test the code. Using Laravel newer https://laravel.com/docs/8.x/http-client I can then use it like this

Http::get($follower->profile_pic_url)->body();

Which looks like this to get the file and save

$contents = Http::get($follower->profile_pic_url)->body();
Storage::disk("local")->put($path, $contents);

and then mock it in a test


Storage::shouldReceive("disk->exists")->once()->andReturn(false);
Storage::shouldReceive("disk->put")->once();
Http::shouldReceive("get->body")->once();
1 like

Please or to participate in this conversation.