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
What about Guzzle get and then save it to where ever you want while unzipping with some sort of package/command?
Hi Joe,
Can i put file using laravel storage class using file_put_contents after?
Tks.
bashy
i did not know guzzle. I will view it.
tks
http://docs.guzzlephp.org/en/latest/
A lot more options other than just doing file_get_contents() and trying to catch all the errors.
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
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( )
@mehany but using curl_exec sometimes brings the value of $url to null
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.
Looks like they have protection on their server for crawlers.
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.
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();
Please or to participate in this conversation.