Mobile browsers are adding .html to filename on download - php

For some reason, with this code:
header("Content-Type: text/x-vcard;charset=utf-8;");
header("Content-Disposition: attachment; filename=card.vcf");
header("Pragma: no-cache");
header("Expires: 0");
echo $vcard_serialized;
on chrome from Pc, it downloads card.vcf, but from mobile it downloads card.vcf.html... why?

I have the same issue, but now I already fixed it using the codes below:
header('Content-Description: Download vCard');
header('Content-Type: text/vcard');
header('Content-Disposition: attachment; filename='.$your_filename_here);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
echo $vcard_serialized; //echo the content
exit;

Related

File download not working show wired character wordpress

I Want to add download file in the site, but when I click on download It shows weird character. I dont know what the error is. I have added type application/octet-stream in my code. I think there is problem of permission but I have also set permission in c-panel :
Here is my file download code:
if(isset($_REQUEST["file"])){
$file = urldecode($_REQUEST["file"]);
$filepath = ABSPATH.'/wp-content/themes/xorisk/document_upload/'.$file;
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header("Content-Type: mime/type");
header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;

PHP download script returns download-file.php instead of filename

i wrote a download script that find a restricted file from server and after reading that, prepare the file for user to download it with the following headers.
header('Content-Type: application/octet-stream');
header("Content-Length: $filesize");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
but sometimes with default browsers download managers, it returns download-file.php instead of filename!
for example it should return abc.zip but it returns download-file.php and download process works perfectly
any help will be appreciated
Since I don't know how you're using $file or $filesize try this instead:
$file = "abc.zip"; // adjust accordingly
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($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
header("Content-Description: File Transfer");
#readfile($file);
exit();
plus, a missing readfile()
This line:
header("Content-Length: $filesize");
should read as:
header("Content-Length: ".filesize($file)); // or $filesize in your case.
Missing filesize()

PHP force header download not working on zip files

I have a document download site where I track files downloaded.
My problem is getting zip files to download through the script I am using. Every other file gets forced to download apart from zip files. When a zip file is pushed it just goes to the download.php page where the script is with no file being pushed out.
ob_start();
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;
Here is a code snippet that works for me:
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file_url).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($file_url));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file_url).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($file_url));
}
readfile($file_url);
Had similar problem, this quotes helped that time:
header('Content-Disposition: attachment; filename="'.basename($file).'"');
I was not using the base_name for the content disposition and length, I had the full path instead. Using base_name worked for me.

Downloading file is corrupted - header

I have be trying to figure out what is wrong but every time i download the image and try to open it, it says that the file is corrupt.
$h is the path which is pulled from the database, the $h displays the image on the page successfully but I dont get why it wont download. Any ideas ??
header("Pragma: public"); // required
header("Cache-Control: private",false); // required for certain browsers
header('Content-Length: '. filesize("../".$h));
header('Content-Type: application/octet-stream');
header('Content-Disposition: inline; filename="'.md5($h).$ext.'"');
header('Content-Transfer-Encoding:binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile("../".$h);
Maybe try to add the following 2 commands before your readfile line.
ob_clean();
flush();
readfile($file);
These lines were in the example for the PHP docs on readfile.
Try this:
$localPath = realpath("../$h");
if (!file_exists($localPath)) {
exit("Cannot find file located at '$localPath'");
}
header('Pragma: public'); // required
header('Content-Length: '.filesize($localPath));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.md5($localPath).'.'.$ext.'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
readfile($localPath);
exit;

How can the image and pdf files be made downloadable

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;

Categories