I am struggling to send a Word .docx file to the browser. The Word file opens fine, if I create a link to the Word file, it also opens fine.
However when using this code:
// output the file to the browser
if ($fileToOutput) {
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
//header('Content-Length: ' . filesize($fileToOutput)); // this is causing the file download to fail
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($fileToOutput) . "\"");
ob_clean();
flush();
readfile($fileToOutput);
exit;
}
I get the following:
Followed by the dreaded "Word experienced an error trying to open the file.":
Like I said, the Word file opens fine, and downlaods from a link fine - I only get these errors when trying to output the Word file with the above PHP code.
Where is this PHP code going wrong?
Related
None of the already existing questions have helped me.
I'm trying to force download an excel file with the file type .xlsx. It works perfectly fine in a download code like this:
echo "<a href='" . $dateiname . "'>Datei herunterladen</a>";
But whenever I try to make a forced download, it doesn't work. I've tried various headers from various questions on stackoverflow, the last two being
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $dateiname . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: no-cache');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');
and
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Transfer-Encoding: binary ");
header('Content-Disposition: attachment; filename='.basename($dateiname));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');
I tried both in the same file as where I create the file, but also in a separate file, but either way I always get the error:
Excel cannot open the file (filename) because the file format or file extension is not valid. Verify that the file has not been corruted and that the file extension matches the format of the file.
The file itself on my server seems perfectly fine.
I've found how I used PHPExcel to force downloading an xlsfile.
header("Content-Disposition: attachment; filename={$fileName}.{$fileFormat}");
header("Content-Type: application/vnd.ms-excel;");
header("Cache-Control: max-age=0");
$objWriter->save('php://output');
Hope it helps.
I referred to these questions and implemented their answers but none of them is working for me.
download mp3 instead of playing in browser by default?
PHP Streaming MP3
content type for mp3 download response
download mp3 from web url with android failed
Basically when the user clicks a download link on the website, we want the user to be prompted to download the audio file.
Code :
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: $mtype");
header("Content-Disposition: attachment; filename=".$file_name);
readfile($fname);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
Here, $mtype is "audio/mpeg"
Success :
It's working fine on all desktop browsers.
I tried with a BlackBerry, and I got the downloaded file.
Failure :
when i do same with android browsers, I did't get the downloaded file.
file name also missing, comes with <untitled> and shows the status "download unsuccessful".
Thank you if you have any idea why this code doesn't work on an Android phone.
Having the same problem and it could be that ur server does not allow you to use mp3 files properly ( I use Biz.nf )
I have just found solution of this problem. On my android Xiaomi it works perfectly.
if (file_exists($file)) {
echo "OK";
if (ob_get_level()) {
ob_end_clean();
} // clearing output script buffer
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
header('Content-Description: File Transfer');
header('Content-Type: {$mime_type}');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
i have the code to upload xml file using dom.
if (!$dom->load($folderName . "/" . $fvalue)) {
echo "<br>".$fvalue." file is corrupt or Tags are not closed properly ";
}
it uploads the file BUT after uploading, there are 3 whitespaces in the start of the file before any xml Tag. Problem is we download uploaded file and upload again. but with whitespaces in the start of the file, dom dont allow to upload the file as it is corrupted.(i check the validation of xml using xmlvalidation.com) which returns error because there are spaces at the start. if i remove space manually than no error.
How i can upload the file without adding spaces at the start ?
I checked to remove spaces from file etc. but i dont want to do that. i want to upload the file without changing.
problem was with downloading the file. after download there was some spaces in the start of the file. code was
header("Content-type: application/force-download; charset=utf-8");
header("Content-type: application/force-download");
header("Content-disposition: attachment;filename=" . $downFileName);
readfile($filename);
i changed to this and it's working fine now.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downFileName));
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($filename));
ob_clean();
flush();
readfile($filename);
I save many documents outside the webroot.
I want to click a link, that opens a new window (target="_blank"), and force download the file that's found.
Here's what I've got so far, but my results show gobble-de-gook in the browser popup, rather than forcing the download to the desktop:
function download($filelocation){
$filename = basename($filelocation);
if (file_exists($filelocation)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
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($filelocation));
ob_clean();
flush();
readfile($filelocation);
exit;
}
}
In the new browser window I simply call that download() function with a specific path the the file.
It's definitely finding the file, but now I'm just wondering what I'm missing with header() to force the file through the browser.
Missing this:
header("Content-Type: application/force-download");
I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc).
Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content.
I wonder that if there is any mistake in this code (or problem when uploading?).
$size_of_file = filesize($download_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
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: ' . $size_of_file);
//read file from physical path
readfile($download_path);
Did you try like this ?
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.