Why does Safari/WebKit appends .html to downloaded files? - php

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?

Related

Download file .ZIP with PHP automatically

I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.
Here is my current script, but it downloads an invalid corrupted file.
$filename = "dsk.zip";
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/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
#readfile("./zz/".$filename);
Can you please help me? The file, dsk.zip, is in the folder zz.
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='zz/'.$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile('zz/'.$file);

Pseudo Streaming MP4 with php

i am trying to make the video seek using php file
this is my code
<?php
$path = 'http://exmaple.com/video.mp4';
$mm_type="video/mp4";
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
");
readfile($path); // outputs the content of the file
exit();
?>
the video work but it is not possible to seek
Seeking requires that your server support handling of the Content-Range: header

Downloading A File Located Above public_html

I have a document hosted in a folder above public_html for security reasons - moving the files into the web root is not an option.
I'm trying to access them via a readfile() but whatever I download winds up being 0 bytes and an empty file. I think the problem is that the "Content-Length" header isn't getting a correct value because filesize($target) is giving no output.
Any thoughts? Code included:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file_url);
I don't see $file_url defined in your code. Maybe it should be $file:
header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file);
I also don't see $filename defined.

In php Instead of downloading csv file it gets open in the browser

I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.
Here is what i tried so far:
if(isset($currency)) {
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=Pricelogs.csv");
ob_clean();
$filename = "/tmp/".uniqid().".csv";
exportCSVFile($filename, $currency, $country, $provider);
readfile($filename);
//unlink("'".$filename."'");
} else {
echo "ERR_USERNAME_PASSWORD";
exit();
}
I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes.
I also tried header("Content-Type: text/csv");
Also:
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/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.
http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv
I usually just do:
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");
// print CSV lines here
exit();
I can tell you this combination is working for me:
$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;
If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.
You might try this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
print $content;
For large files you need to get your output buffer started
add : ob_start(); at the start
ob_clean(); at the end of you file

php force download extension

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"); ?>

Categories