can anyone know on how could I force to download a file without displaying DialogBox (Open/Save). The below code was is my test script to download created excel file but the dialog box appears to download a file.
$filename ="excelreport.xls";
$contents = "testdata1 \t testdata2 \t testdata3 \t \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
I want to automatically download file and save it on specified directory without dialog box
You can force to download instead of showing it but I think it is not possible to force the browser in general to download it without a prompt
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
Source: php.net
You cannot do this for security reasons.
Related
I am using the following headers to force a download but I need to try and have the browser display certain files like PDF's and JPG's if that is the file type, finding the exntension is easy enough but how can I alter these headers to open the file in the browser?
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header);
header("Content-Transfer-Encoding: binary");
header("Expires:0");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-length: $filesize");
In order to display a file in the browser, you'll need to use the correct MIME type. You can set it yourself based on file extension, or you can use the finfo module:
function getContentType($filename) {
$finfo = new finfo(FILEINFO_MIME);
return $finfo->file($filename);
}
header("Content-Type: " . getContentType($filename));
Without this, the browser will probably assume that it can't handle the application/octet-stream content, and force a download anyway.
You should also only send the Content-Disposition header if you want to force the file to be downloaded. If you remove that header, then the browser can decide if it should display the file or download it.
I have a zip files that I want users to be able to download. The trick is I don't want the users to see what the url is and I don't want to download the file to my server.
So I want users to click a link like this:
http://example.com/download/4
which server-side accesses my S3 bucket with this url:
https://s3.amazonaws.com/my-bucket/uploads/4.zip
I've tried cURL, using S3 methods, and various headers() in my download($file_id) function but can't get this to work. This has to be easy, right?
Your right, its quite easy. Probably you will have to write something like this:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
You set various headers to make your user download the .zip. Afterwards you put your file into the output buffer with readfile() Afterwards you end your script with exit() for security's sake. This should work for you! Remember to change the path to your file.
Thanks #Xatenev for the help. This is actually what worked for me:
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/zip"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
readfile($path); // outputs the content of the file
exit();
I can't seem to figure this out and I know it's something simple. I am building the back-end to a very basic content management system. For this specific piece, I am just trying to create a PHP link that allows for a file (the client's CV) to be downloaded.
MY PROBLEM:
When the link to download the file is clicked, instead of the browser prompting you to choose a local directory to save the file to - it simply displays the file and a bunch of symbols before and after the document's contents (I am assuming this is the file's opening and closing exif data for an application to decipher).
How could I go about forcing the browser to prompt the user for a "Save As..." box?
<?php
require("connect.php");
$query = "SELECT * FROM kb_info LIMIT 1";
$result = mysql_query($query, $link);
while ($row = mysql_fetch_array($result)) {
$file_extension = end(explode(".", $row["extension"]));
if ($file_extension == doc) {
header('Content-disposition: attachment; filename='.$row["extension"]);
header('Content-type: application/doc');
header ("Content-Length: ".filesize($row["extension"]));
readfile($row["extension"]);
exit;
}
if ($file_extension == docx) {
header('Content-disposition: attachment; filename='.$row["extension"]);
header('Content-type: application/docx');
header ("Content-Length: ".filesize($row["extension"]));
readfile($row["extension"]);
exit;
}
if ($file_extension == pdf) {
header('Content-disposition: attachment; filename='.$row["extension"]);
header('Content-type: application/pdf');
header ("Content-Length: ".filesize($row["extension"]));
readfile($row["extension"]);
exit;
}
}
?>
Many thanks,
Joshie
I think the problem can be that there is some whitespace somewhere in the PHP files, which causes that the headers are not sent correctly and therefore you see the whole output.
I would suggest the followings steps:
check the "connect.php" and look for empty lines/spaces at the begining/ending of the file and remove them
adapt you php files that way, that you leave out the ending tag ?> at the end of the file - that way you do not get empty lines at the end of the file
if the above are not enough you need to check your apache and php error log and/or set up error loging, so you see also warnings - that you you would be informed if the headers are not sent correctly or if there is some other error
Headers I use for download:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$file);
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$bytes."");
I have a download.php file with this source:
<?php
$filename = $_GET['file'];
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$filename}");
header("Expires: 0");
header("Pragma: public");
readfile($filename);
?>
This is just for demonstration sake. It can stream a file and show a download prompt. But this only works with one file. What if I want to stream two or three files? Is there any more elegant solution than to keep redirecting the page two or three times until all files are downloaded?
You could have an html page with multiple iframes, each iframe pointing to a download url.
It's not possible to stream multiple files, not cross-browser in any case.
You could stream a zip-file with multiple files though.
I am dynamically generating image in php. The image has a fixed name. I wants a button or hyperlink and onclick of that button, users should be able to export image rather than right click and save as image options. The problem is that in case of excel,pdf or doc files, I can specify the path of file and browser automatically asks for the open or save option but for images, it opens them in separate window.I want same dialog box for saving the image as for the other files like excel,pdf.Please help me on this.
Thanks
you can set the header to force download (for PHP):
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: image/png");
Then you can read the file
readfile($file);
What you can do is force image download using .htaccess if you are using Apache or directly via php using header tags
PHP Example
$file = "PATH TO FILE" ;
$fileName = basename($file);
$fileSize = filesize($file);
set_time_limit(0);
header("Pragma: public");
header("Expires: 0"); // Cache Options
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\" " . $fileName ."\"");
header("Content-length: $fileSize");
readfile($file);