I am trying to download a file using PHP. Now the file is downloaded , but it is not getting in the original format (extension missing). I can use the downloaded file after rename it using the original file extension. I am using the following code
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/".$result['ext']);
header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename='.$result['file_name']);
readfile($file);
exit;
where
$result['ext']="rar",
$file="file path to the uploaded folder".
I'm pretty sure you have to send the extension to the browser in the file name as part of your Content-disposition header.
For your code, you would have to change this:
header('Content-disposition: attachment; filename='.$result['file_name']);
to:
$filename = $result['file_name'] . '.' . $result['ext'];
header('Content-disposition: attachment; filename=' . $filename);
Related
I want to download the zip file in php.
Here is my code below.
<?php
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>
And i have a link in my html code.
<html>
<head>
</head>
<body>
Download
</body>
</html>
The file is downloaded successfully when i clicked the link named 'Donwload' but i can't unzip and open the file.
The file name is test.zip when i downloaded for the first time and it is just the file name. Its extension is created when i click it to unzip.
The extension of file is ". cpgz" not the ".zip".
Here is the log when i downloaded it.
Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"
Did i do something wrong in my code? or miss something?
How can i fix this?
My uploaded file is already zipped in the server and all i want to do is just download it.
The problem is your filepath, there's a missing trailing "/" after "/home/somewhere/file/zip" the working directory should be:
$filepath = "/home/somewhere/file/zip/";
Now this code works great!
<?php
$filepath = iconv("UTF-8","CP949", "/home/somewhere/zip/test.zip");
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Content-Transfer-incoding: utf-8");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));
ob_end_flush();
readfile($filepath);
?>
i want to create a link to download an excel file from the root in server computer, using php. so i wrote a simple code as below,
<?php
header("Content-disposition: attachment; filename=example.xls");
header("Content-type: application/vnd.ms-excel");
readfile("example.xls");
?>
it then can be downloaded however when i want to open it, i got the error saying the file i downloaded is in a different format than specified by the file extension. i also tried the same method with jpeg file and didnt get the same error but when i click it, it shows nothing. can someone help me? im not very good with programming. thank you in advance!
Try this
$file='example.xls'; $filesize=filesize($file);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="'.basename($file).'"');
header("Content-Length: " . $filesize);
$fh = fopen("$file, "rb");
// output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);
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?
I am working with some legacy PHP code and have run into Word Doc (docx) and Spreadsheet (xlsx) corruption.
Here is the current code in the download.php file:
$new_file_name = stripMySlashes($filename);
header("Content-disposition: attachment;filename=$new_file_name");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
$client=getenv("HTTP_USER_AGENT");
$fp=fopen('uploaded_files/'.$folder.'/'.$filename,"r");
$str=fread($fp,filesize('uploaded_files/'.$folder.'/'.$filename));
echo $str;
fclose($fp);
How can I avoid checking for a bunch of filetypes in a case statement for example? I tried code like this with no luck
$file="test.docx";
header("Pragma: public");
header('Content-disposition: attachment; filename='.$file);
header("Content-type: ".mime_content_type($file));
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($file);
Any help is extremely appreciated. Thanks
Props to How to download word file using php on chrome and IE
I used this to fix the docx corruption issue...
$new_file_name = stripMySlashes($filename);
$fdl = #fopen('uploaded_files/'.$folder.'/'.$filename,'rb');
header("Status: 200");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header("Pragma: hack");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Type: application/octetstream");
header("Content-Disposition: attachment; filename=\"".$new_file_name."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize('uploaded_files/'.$folder.'/'.$filename));
if($fdl)
{
while(!feof($fdl)) {
print(fread($fdl, filesize('uploaded_files/'.$folder.'/'.$filename)));
flush();
if (connection_status()!=0)
{
#fclose($fdl);
die();
}
}
}
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"); ?>