Reputation: 6098
I've got a URL of a pdf or a an image. I want to get that file and store it on my server.
I know laravel has File::get(), but can this open a file from a web url and then do File::put() to a a folder on the server?
Is there a better way to achieve this?
thanks!
Here is what I have:
$shared_folder = $destinationPath.'/shared'
if(!File::isDirectory($shared_folder)){
File::makeDirectory($shared_folder,0777,true,true);
File::get($attachment->link);
$file_name =$file->getClientOriginalName();
File::put($shared_folder,$file);
$new_location = $shared_folder = $destinationPath.'/shared/.'.$file_name;
}
Upvotes: 1
Views: 1061
Reputation: 60058
I know laravel has File::get(), but can this open a file from a web url and then do File::put() to a a folder on the server?
No - this will only access a local resource.
You need to use cURL and access the resource. I suggest using the Guzzle package which allows for an easy way to use cURL.
Then you could do something like this
function getFile($fromUrl, $toFile) {
$client = new Guzzle\Http\Client();
$response = $client->get($fromUrl);
->setResponseBody($toFile)
->send();
return true;
}
Upvotes: 1