xxxxxxxxxx
<?php
$url = 'http://example.com/file.txt'; // Replace with the URL of the file you want to download
$destination = '/path/to/save/file.txt'; // Replace with the desired destination file path on your server
$fileContents = file_get_contents($url); // Get the contents of the file from the URL
if ($fileContents !== false) {
file_put_contents($destination, $fileContents); // Save the file contents to the destination path
// Output a success message
echo 'File downloaded successfully!';
} else {
// Output an error message if file retrieval failed
echo 'Failed to download file.';
}
?>
xxxxxxxxxx
<?php
$url = "https://example.com/image.jpg";
$imageData = file_get_contents($url);
file_put_contents("/path/to/save/image.jpg", $imageData);
?>
xxxxxxxxxx
<?php
if(isset($_REQUEST["file"])){
// Get parameters
$file = urldecode($_REQUEST["file"]); // Decode URL-encodedstring
$filepath = "images/" . $file;
// Process download
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit;
}
}
?>
xxxxxxxxxx
$c = curl_init('https://yourURLhere.com');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
xxxxxxxxxx
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.)
xxxxxxxxxx
<a class="btn btn-outline-primary" href="{{ asset('excel/application-user-sample.xlsx') }}">
<i class="fa fa-download mr-1"></i>Download Sample File
</a>