I try to force a file-download via PHP with
$ctype="application/zip";
header("Content-Type: $ctype");
header("Content-Length: ".filesize($filepath));
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //header("Cache-Control: public");
header("Pragma: public");
header("Content-Disposition: attachment; filename=".$filename);
// header("Location: $filepath"); // edited: removed
readfile($filepath);
but it doesn't work.
with firebug I can see the changed header information but no save-file dialog appears...
You need to remove
header("Location: $filepath");
Which is basically redirecting you to the path specific instead of reading it's contents.
sendAndLoad() will "eat" your response and not cause a download which is useful if you're trying to load data. However, in your case you need to use getURL() since you want the browser to deal with the response, not flash.
Related
I have a PHP script called retrieve.php that compresses uploaded files from a directory in to a .zip file and then outputs them to the stream, so said file can be downloaded.
The script works in all browsers except Firefox.
After searching online for quite a while now and coming across this question: PHP download script returns download-file.php instead of filename and still having the same issue, I'm stumped and have no clue what's going wrong.
This is the code being used to download the file:
if ($zippedElements >= 1) {
// die($zipFilename);
$zipFilename = "download-".time().".zip";
// Send zip to recipient
header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
header("Content-Length: ".filesize($zipFileName));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header("Connection: Keep-Alive");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Accept-Ranges: bytes");
readfile($zipFileName);
} else return;
I've tried setting the header to different values, such as content-type: application/force-download, adding the asterisk (*) after filename, etc., but all to no avail.
The result in Firefox looks as follows:
Any help with the matter would be much appreciated.
You have inconsistencies in the variable name $zipFilename - the N is uppercased sometimes.
$zipFilename = __FILE__;
// Send zip to recipient
header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
header("Content-Length: ".filesize($zipFilename));
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
header("Connection: Keep-Alive");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Accept-Ranges: bytes");
readfile($zipFilename);
binaryhere is my problem
I'm working with php, I create a zip archive thx to ZipArchive library.
I complete it with all files I need and save it on my server.
Then I want to make it download by the current user, here is what I wrote :
if (file_exists($zip_archive))
{
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, 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=\"".$zip_archive."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zip_archive));
ob_end_flush();
readfile($zip_archive);
}
But when I download the zip archive, I can see files inside, but when I try to extract them, an error occurs telling me that the file might be corrupted.
So I tried to open the zip archive saved on the server, and NO error appears.
Have you got any idea to help me ?
Thanks for any help.
(I changed fichier to binary, but a problem persists : End of archive ...)
This line:
header("Content-Transfer-Encoding: fichier");
Should read:
header("Content-Transfer-Encoding: binary");
You can read more about this header at http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html. There is also a list of available encoding types, suffice to say 'fichier' isn't there.
I have a php-download script that looks like this:
<?php
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
?>
Normally with this I can force my E-Book-Reader (a Sony PRS-T1) to download a file. However, if I use the same script inside of a folder with .htaccess-authentication, the download fails.
On my computer, the download works, no matter if there is a .htaccess-authentication or not.
Can you help me to find the reason for this behaviour of my Ebook-Reader and make downloads inside of protected folders possible?
Thank you!
[edit]
The PRS-T1 runs Android. Maybe this issue (link) is the answer - it is simply not possible...?
I am building a web application using Yii. The application generates MS-Word documents. Does anyone can give me a hint on how to force the browser to open a save as dialog.
Thanks
You will need to send these headers:
header("Pragma: no-cache"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=\"test.doc\"");
header("Content-Transfer-Encoding: binary");
This sends the file as test.doc download.
I would to know the command in a PHP script to get in output and save a file from my site.
Thanks
See here for a good description of how to force the output of a php script to be a download.
The basics of it are:
// Set headers
header("Cache-Control: public");
header("Content-Description: File Download");
header("Content-Disposition: attachment; filename=" + $filename);
header("Content-Type: application/zip"); // or whatever the mime-type is
// for the file you want to download
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($full_path_to_file);
As an addition (provided by Gordon's comment), see the 1st example on the php documentation here
At the End of the files or used in clicking files, you can add this
$filesh = "check.xls";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filesh));
header("Content-Description: File Transfer");
readfile($filesh);
if you got any header using problem means, top of the file you can add ob_start(); function
If you mean getting output, contents from other site or location, this what you need file_get_contents