I'm trying to force a download so this is my code:
$file = 'test.m4r';
$mime = 'audio/aac';
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-Type: $mime");
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);
The file extension should be .m4r, even though the mime is aac. On some computers it's downloaded as test.m4r, while on other computers, the file has extension of test.m4r.acc. How do I fix this problem?
Thank you!
You can lie about the mimetype, but besides that there isn't anything you can do.
Try:
"application/octet-stream"
This might work, and is the default for unknown filetypes etc.
try this one
<?php header("Content-Type: application/force-download"); ?>
Related
i have a code like this :
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-type: application/rar');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename='.$fullname);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($fullname));
$fp = fopen($fullname, "r");
fpassthru($fp);
fclose($fp);
based on above code, download is running, but the tar.gz cannot be opened.
how to solve it? is it possible to download tar.gz?
Content type for GNU zipped archive is application/x-gzip.
You should use below code to set content type
header('Content-type: application/x-gzip');
I'm trying to force a file download and all I get is a random string of unknown characters in the browser window. Code is included:
$file_url = "docs/$c/$path";
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$title.$type");
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-Transfer-Encoding: binary");
header("Pragma: public");
header("Content-Length: ".filesize($file_url));
readfile($file_url);
It's been going on for about a week, I'm going nuts. Any help is appreciated.
Thanks.
I suspect your Content-Disposition header is the problem.
Try this version instead:
header("Content-Disposition: attachment; filename=\"$title.$type\"");
I was facing the same error. Using ob_clean() and flush() helped my case. I used the code below:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename='.$filename.'.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-type: application/force-download');
header( 'Content-transfer-encoding: binary' );
ob_clean();
flush();
readfile('Reports/'.$filename.'.pdf');
exit;
I have the following code in an attempt to have it so a specific file on the server is downloaded:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename="basename.$file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
All that happens is it causes the browser to download a file, in my case "Order.txt", but its blank, it is just creating this file out of no where. It doesn't do anything with the "upload/" path portion of $file.
Thanks for any help, I find headers really confusing, why I can't just have "download ($file)" is beyond me.
Edit: I'll struggle over a problem for days, finally decide to ask here then immediately after fix it my self.
I changed the location of the file to be in the same as the PHP script, I'd still like to know how to make it work with them being separate however. I also added:
readfile($file);
If I alter with of these 2 changes it doesn't work.
Also slim lined it, new code:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
you should print content of file to stdout after headers:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
echo $contents;
Try change this line:
header("Content-Disposition: attachment; filename='".basename($file)."';");
I'm having lot of issues with Safari. When I provide a download from a PHP script the downloaded file has ".html" extension appended. Despite the HTTP headers I send to the client, I found them changed in inspector; in particular the mime-type is always set to text/html.
Here what I'm sending to the browser:
<?php
....
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"{$file['name']}\"");
header("Content-Type: ".$file['type']?$file['type']:'application/octet-stream');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file['file']));
ob_end_flush();
#readfile($file['file']);
Where $file contains:
$file = array(
'name'=>'myfile.zip',
'file'=>'/tmp/myfile.zip',
'type'=>'application/zip'
);
What can I do?
Generally, browsers show the image and pdf files without embedding them in html. I need some codes to make these files not to show in the browsers but make them downloadable like doc files.
Please help me out with this.
This isn't up to you, it is up to the browser.
However, you can make a suggestion as to what to do with it by setting the content-disposition header...
header("Content-Disposition: attachment; filename=\"yourfilename.pdf\"");
Read the doc on the header() function: http://php.net/manual/en/function.header.php
In case this isn't clear... this is for whatever resource is returned by the PHP document. You may need a readfile() in there to do what you are trying to do.
Set a couple of headers:
$filename = ...;
$mime_type = ...; //whichever applicable MIME type
header('Pragma: public');
header('Expires: 0');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($filename));
readfile($filename);
<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?>
You want to send a content type header to make the browser download the file.
If you aren't' generating it dynamically, you will need to read it off the disk first.
$fullPath = "/path/to/file/on/server.pdf";
$fsize = filesize($fullPath);
$content = file_get_contents($fullPath);
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-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
echo $content;
try this one :
$file = 'youfile.fileextention';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;