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);
?>
Related
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.
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:
PHP generate file for download then redirect
(11 answers)
Closed 9 years ago.
I use the following code to download an xml file
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="download.xml"');
echo simplexml_load_file('temp.xml');
unlink('temp.xml');
?>
I would like to redirect to index.php after this. How do I do this?
header('location...) and meta refresh do not work
Well because you send the header and content this can't be done.
You have to redirect first and then let the file download on the target site.
Make your website change the url two times with javascript:
<script>
location.href="your_php_file_that_downloads_xml.php";
location.href="/other/site";
</script>
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:
How to Automatically Start a Download in PHP?
(5 answers)
Closed 7 years ago.
I have a php function for getting info out of my database. When they go to http://example.com/test/download
I want to create a fake test.txt (text is dynamic) and download it. It's contents should be the equivalent of executing foreach(databaseContent() as $content) { echo $content . '<br/>' } inside of it.
How can I get started on this? (Using php)
You can link to a php document along these lines, which forces a download of type plain text. (Well, suggests to the browser that that should happen, at any rate.)
<?php
header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');
echo "this is the file\n";
echo " you could generate content here, instead.";
?>
Of course, pass in appropriate post or get args, to control it the way you like.