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
How to Repair a Serialized String Which Has Been Corrupted by an Incorrect Byte Count Length
Unicode Character in PHP String
How to Get the Cookies from a PHP Curl into a Variable
Return Single Column from a Multi-Dimensional Array
How to Implement an Access Control List in My Web MVC Application
File_Get_Contents(): Ssl Operation Failed With Code 1, Failed to Enable Crypto
How to Convert Xml into Array in PHP
How to Get Parameters from a Url String
Convert a PHP Script into a Stand-Alone Windows Executable
How to Create Websockets Server in PHP
Create a CSV File For a User in PHP
Automatically Deleting Related Rows in Laravel (Eloquent Orm)
Replace Keys in an Array Based on Another Lookup/Mapping Array
PHP MySQLi_Connect: Authentication Method Unknown to the Client [Caching_Sha2_Password]