简体   繁体   中英

How do I open a file from a URL in Laravel 4

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 aa 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; }

I know laravel has File::get(), but can this open a file from a web url and then do File::put() to aa 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM