Can't download a word file - php

Can you tell me what is wrong with my headers? My download is not effective. I use PHPWord library but I don't think it's a problem
<?php
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$wordText = utf8_encode($_REQUEST['TEXT']);
$section->addText($wordText);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//$objWriter->save('helloWorld.docx');
$path = 'tmp/kikou2.docx';
$objWriter->save($path);
//download code
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .$path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($objWriter));
readfile($objWriter);
unlink($objWriter); // deletes the temporary file
exit;
?>
Thanks

Recommends headers for an OfficeOpenXML .docx file
// Redirect output to a client’s web browser (.docx)
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="kikou2.docx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
paying particular attention to the Content-Type heading

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.basename($objWriter).'"'); //<<< Note the " " surrounding the file name
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($objWriter));

Add two headers:
header("Content-Type: application/force-download");
header("Content-Type: application/download");

$document->save($url.'temp/Form Letters1.docx');
$path = $url.'temp/Form Letters1.docx';
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="Form Letters1.docx"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);

Related

php - download a cookie together with pdf

I need to download a cookie together with PDF. I added setcookie to following function but it doesn't work. What's wrong? Thanks
protected function send_download($file) {
if (file_exists($file)) {
set_time_limit(0);
header('Connection: Keep-Alive');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
setcookie("fileDownloadToken", "fileDownloadToken", time()+360); // 10 minutes
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);
}
}
Resolved :)
It was visible only as response cookie. It was necessary to set path and domain.
setcookie("fileDownloadToken", $fileDownloadToken, time()+360, '/', $this->template->domain); // 10 minutes

Save Mysql data in xls using php

I want to export data in xls format using PHP. But instead of download information, they are displayed in my web page.
//connection to database
//building of all information -> ($righe)
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: esporta_utenti.xls");
print $righe;
exit();
$righe is my tuple
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // file mime type
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
This code was originally Published here http://davidwalsh.name/php-force-download . And can be referred for a detailed explanation of the Code.

Download xlsx file using php

I need make xlsx file download from my site (but not from directly open file url like this: http://site.com/file.xlsx )
So, this is php code
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
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));
ob_clean();
flush();
readfile($file);
file is downloaded, his extension is .xlsx, but when trying open this file in ms excel, file not opened and I got error : excel cannot open the file.xlsx because the file format or file extension is not valid
Tell please, why this happened? where I am wrong?
After many years, I got same problem, and after searching, I got here again ))
This is solution, that worked for me:
$file = "somefile.xlsx";
// define file $mime type here
ob_end_clean(); // this is solution
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($file);
You must be using this code in middle of some other file.
The problem with headers is they need to be set first on a page. They will not work if you have even 1 single space echoing before them. So you need to ob_clean() [clean the buffer] before you are setting headers
Try
ob_clean();
flush();
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
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);
Remove:
ob_clean();
flush();
Add at the end of code:
exit();
The issue is that flush() will also throw in your *.xlsx file content some garbage it has in it and that will corupt your file, even if you use ob_clean();
For a better understanding go to php.net and read the difference between flush(), ob_flush() and find that you didn't even need them in the first case. Therefore you won't need the ob_clean() too.
This works for me:
header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".basename($fileLocation)."\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Length: ' . filesize($fileLocation)); //Remove
ob_clean();
flush();
readfile($fileLocation);

not supported or damaged pdf/doc file when downloading in php

I am downloading PDF/DOC files using php
This is my html code :
<a title="Download" target="_new" href="includes/pdf_server.php?file=test.pdf">Test PDF</a>
This is my php code in pdf_server.php file
<?php
$file = $_GET["file"];
if (file_exists("../PDF/".$file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(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("../PDF/".$file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
PDF is the folder where I have my test.pdf file . When I click on link to download file. Browser shows file to download with its size (1.4 mb) but when download finished and I open the file it shows error either file damaged or not supported file. Then I check it's properties it shows 0 bytes.
Please help
Try this-
<?php
$file_name = $_GET["file"];
if (file_exists("../PDF/".$file_name)) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ("../PDF/".$file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.'application/pdf');
header('Content-Disposition: attachment; filename="'.basename("../PDF/".$file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile("../PDF/".$file_name); // push it out
exit();
}
?>

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.

Categories