This question already has answers here:
Make multiple files to force-download
(4 answers)
Closed 4 years ago.
I have a problem.
I want to download multiple images form urls to visitor device. I dont need to zip the files. Just download one by one
This is my code :
$files = array(
'https://example.com/image.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
);
foreach($files as $file)
{
$filename = 'images.jpg';
function forceDownload($filename, $type = "image/jpeg") {
header('Content-Type: '.$type.'; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'"');
}
forceDownload($filename, "image/jpeg");
echo file_get_contents($file);
}
Thanks
You cant, HTTP doesn't provide a mechanism for sending multiple files over one request.
the only sensible way you can do this is to zip the images up into one zip file and download that.
OR...
display the images on the page and have a download button for each one individually I suppose.
Also your code is wrong, you are setting the file name to images.jpg and declaring a function every time you loop through the foreach loop.
Related
This question already has answers here:
Prompt user to save file to a specific location
(4 answers)
Closed 6 years ago.
Here I am generate the dot RTF file for $report_content content.
It's successfully generated dot RTF file.
No need file download option and I want to move some location.
How to do?
<?php
//load the rtf template as a string
$email_id = 'ram#gmail.com';
$file_dir = 'uploads/';
$report_content = "
<html>
<body>
Hello, This is testing<br /><br />
message.
</body>
</html>";
//and now serve the file as an rtf download:
echo $report_content;
header("Content-type: application/rtf");
header("Content-Disposition: attachment;filename=rtf.rtf"); //How to save in $file_dir
exit();
Please update any other ways.
1)its not possible due to security issue.
2)you have to suggest the user to save particular location
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Force-downloading, from php file
(2 answers)
Closed 9 years ago.
I am trying to make a php script when you load on to that page it downloads a video. However since i do not know anything about headers it seems i can't figure it out so please explain why it works and how header works. I am trying to make the browser download video files.Can someone also explaain the headers and what they do please.
Here is my failing code:
<?php
//Outputing video name
$file_name = $_POST['FileToD'];
//outputting video extension e.g video/mp4
$file_ext= $_POST['FileExt'];
//where the file is kept
$file_path = 'mysever.myadress.com/media/movies/' . $file_name;
header('Content-Type:'.$file_ext);
header('Content-Length:' . filesize($file_path));
header('Content-Description: attachment; filename='.$file_name);
readfile($file_path);
?>
If you want to output a video, then don't start by outputting HTML and then switch to video data as part of the same file. (You can't set response headers after you've started outputting data anyway). Remove everything before <?php and after ?>
$file_url should be the path, on the server's file system, to the file you want to make available. It shouldn't be a URL (unless you want a really inefficient approach or need to proxy from a different server), and if it is a URL then it needs to start with the scheme (e.g. http://).
The content-type needs to be the actual content type of the video (e.g. video/mp4), not a file extension (and it doesn't make sense for it to be provided by the user).
You also need to sanitise the user data. At present (if the errors described above were fixed) then anybody could request any file that exists on the server.
This question already has answers here:
A PHP script to let users download a file from my website without revealing the actual file link in my website?
(4 answers)
Closed 9 years ago.
I want to make images shopping site in which I want people buy images then they can download them.
My problem is how to create hidden path to image that people download the image and don't know the real path of the image.
You can call a php file to download the image and not the real image/path.
Like this you can call the real path inside your php file with something like:
$path = "/public_html/yourPath/";
if (! isset($_GET['img'])) {
die("Invalid URL");
}
$imageName = filter_var($_GET['img'], FILTER_SANITIZE_STRING);
$finalPath = $path.$imageName;
header('Content-type: octet/stream');
header('Content-Type: image/jpg');
header("Content-Disposition: attachment; filename=$finalPath;");
readfile($finalPath);
You can read more about it here.
Store the images in an offline location (not www) and retreive them with PHP, so they can access the image for example like this: http://yoursite.com/index.php?file=filename and then PHP will go and return that file from the offline location. You just need to set the correct headers so the content is not treated like a web page but an image instead. Now obviously, such link is still public so you need to add some more information to it to authenticate the downloader.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - send file to user
I have written a script that successfully outputs userdata to a CSV file on my server.
My question: is there a way to make the CSV file popup so the user can open it? As is, the only way to open the file is directly from the server.
<?php
$csv_file = '/path/to/yourfile.csv'; // path to the csv file you're generating on the server
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($csv_file));
readfile($csv_file);
?>
This question already has answers here:
Download multiple files as a zip-file using php
(4 answers)
Closed 9 years ago.
I want to make my user select any type of files listed and make them as a zip folder and download it. The files may be .doc,.jpeg,.ppt, etc
you can take a look at ZipArchive, you would be able to create zips with that and let the user download it.
Cletus provide a really good answer there. I humbly copy his sample here
$files = array('readme.txt', 'test.html', 'image.gif');
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);