Download File to Server from Url

Download File to server from URL

Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));

From the manual:

If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using
stream_copy_to_stream().

(Thanks Hakre.)

Download file directly from URL to local folder or a folder in remote server

It looks like $data['attachments'] is a json array so you need something like:

    $attachments = json_decode($data['attachments']);
$api_key = 'APIKEY';
if ($attachments) {
foreach ($attachments as $attachment) {

$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("API:$api_key")
)
));

file_put_contents('/var/www/download_loc/' . $attachment->name, file_get_contents($attachment->url, false, $context));

}
}

Download file from url, save to phones storage

Use https://pub.dartlang.org/packages/flutter_downloader. Don't forget to do platform configurations.

Basically, this is how you should use the package. There is a detailed long example in the link.

final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);

Edit: Some people said the package on top is to well maintained. Try this one
https://pub.dev/packages/flowder

Download a file in Laravel using a URL to external resource

There's no magic, you should download external image using copy() function, then send it to user in the response:

$filename = 'temp-image.jpg';
$tempImage = tempnam(sys_get_temp_dir(), $filename);
copy('https://my-cdn.com/files/image.jpg', $tempImage);

return response()->download($tempImage, $filename);


Related Topics



Leave a reply



Submit