I'm using typical PHP code to download documents:
header('Content-Type: ' . $mimeTypes[$fileext]);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: private');
header('Pragma: private');
readfile($filepath);
Everything works fine - a download/save-as dialog opens, except for .doc files which attempt to open in docs.google.com but they fail due to lack of permission - that's because I'm serving files from outside a website root.
So how do I bypass docs.google and force every browser to offer save-as dialog regardless of the file mime type? ('doc' => 'application/msword')
I tried the following to no avail:
in .htaccess file:
<FilesMatch "\.(?i:doc)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
in .htaccess file:
AddType application/octet-stream .doc
in PHP script:
header('Content-Type: application/force-download');
Add this to your headers:
header("Content-type: application/force-download");
The following php worked for me
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$file."");
header("Content-Transfer-Encoding: binary");
header("Content-Type: binary/octet-stream");
readfile ($link);
P.S. - I used the File extension in the $file variable only, so no need to mention mime type...
Hope this helps :)
Related
I have this code to download a Zip file from the server:
<?php
$path_parts = pathinfo($_GET['a']);
$file_name = $path_parts['basename'];
$file_path = 'temp/' . $file_name;
if (file_exists($file_path)) {
$size = filesize($file_path);
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header('Expires: 0');
header("Content-Type: application/zip");
header("Content-length: $size");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file_path);
}
else
echo 'File does not exists';
?>
The files are stored in a /temp folder and the curious thing is that if I download a TXT file, it downlads OK, but if the file is a ZIP file then it downloads an empty file. Even if I change the extension of the ZIP file to TXT it still downloads empty, but any other file download just fine. What could be causing this behavior?
In fact, it downloads any other file but the ZIP files in the folder.
It may be the file size, not the extension. Look in your php.ini file and see what these values are set to. If your file is larger than these, this would explain it.
post_max_size=5M
upload_max_filesize=5M
The problem is that PHP is not reading the ZIP files generated by the Java App that produces them. But, any other ZIP file is being read.
As I am using Java to create the ZIP files, I had to replace the method by this one posted here (https://www.mkyong.com/java/how-to-compress-files-in-zip-format/). It now works normally.
Try to use
header("Content-type: application/octet-stream");
instead of
header("Content-Type: application/zip");
I used readfile to read the file but its not working for URL path. So, I gave the physical path for it. How to use URL path ?
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($path) . "\"");
readfile($path);
If php.ini has enabled the option called
allow_url_fopen=On
Then url base path will work in readfile other wise you will have to use fopen.
Thanks
Amit
My PHP script forces a file to download, which all works fine, but when forcing zip files to download, they corrupt.
When I try and open the zip file locally it just converts from a .zip to a .zip.cpgz
If I download the file from a direct URL the zip file is fine, so I know it's only corrupting during the push/download phase.
Here's my code
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"_zip_download.zip\"");
header("Content-Transfer-Encoding: Binary");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($zip_file));
For zip file you can set header("Content-Type: application/zip");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"_zip_download.zip\"");
header("Content-Length: " . filesize($zip_file));
readfile($zip_file);
exit;
I have a PHP that scans the files in a remote NAS Hard Disk via FTP protocol, generates a json file and then via javascript I list those files in the browser.
When the user click a link to a mp4, jpg and many browser-known formats, the browser opens te content instead of downloading it.
Now, I know that with PHP or .htaccess I can change the headers to force the browser to download the file but the file is in a remote location and can only be access via FTP so I can't run PHP or .htaccess in it.
I tried this header variations in PHP:
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/image");
header("Content-Transfer-Encoding: binary");
all ending with:
header("Location: $url");
(url being ftp://user:password#dyndns.org/folder/file.mp4)
but it always opens the file in the browser instead of downloading (on recognized file extensions of course)
Any ideas? Thanks
The previous headers may not work when using a redirect. Instead, you better serve them via PHP instead of redirecting:
header("Content-type: octet/stream");
header("Content-disposition: attachment; filename=".$file);
header("Content-Length: ".filesize($file));
readfile($file);
But note, that this WILL use your own bandwidth. However, there isn't any way to force a behavior on a remote host.
I am having a problem with how to download the file into our desired folder, this below is my code for download.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header ("Cache-Control: must-revalidate");
header('Pragma: public');
ob_clean();
flush();
readfile($data);
echo $data;
exit;
I want to download the file to my desired folder like C:\Users\Asus or D:\Program not to the default one
$data is the content of the file and $filename already include the extension Ex:picture.jpg
Thx before for all the help that i can get :)
Henry
There is no way to tell the client where on the client's disk to save a file to.
You have no way of knowing (on the WWW at least) what directories exist
If the user doesn't pay attention to where something is being saved (because they expect it to be the default) then you could save the file somewhere they wouldn't want it (such as a Startup folder) which would be a security risk.
The download directory is usually handled by the browser, not by the server or PHP it's self :)