I have a problem about downloading Excel file from server.
the excel file was already saved on the server and I downloaded it using the code below.
if(file_exists($reportPath)){
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$dirFile[count($dirFile)-1]);
//read from server and write to buffer
readfile($reportPath);
}
But the downloaded file was corrupted.
I'm pretty sure that the file saved on the server is not corrupted since I have get it manually from the server to my local desktop.
Meaning, the data has been corrupted on the fly.
Please help, thank you, I'm using PHP
Can you try these headers?
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$dirFile[count($dirFile)-1].'"');
header('Cache-Control: max-age=0');
And see if it's working... Cheers!
Download script should be separate file. Actually you should not print out anything in this script
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
$fileName = "data.xls";
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
ob_end_clean();
header("Content-Type: application/download");
header('Content-Disposition: attachment;filename=' . $fileName);
$object_writer->save('php://output');
use ob_end_clean() to clear the output buffer.
This is what fixed my issue :
adding ob_end_clean funct after the save.
adding exit at the end of the script.
Related
I'd like to know, how can I realise a Vcard download.
That's my current code:
$path = "../../media/resources/";
$file = "someName.vcf";
header('Content-Type: text/x-vCard');
header('Content-Disposition: attachment; filename= "'.$file.'"');
header('Content-Length: '.filesize($path.$file));
header('Connection: close');
readfile($path.$file);
Unfortunately, it does only give out the content from the .vcf file.
How can I give this vcard to the user as a download?
You have header('Connection: close'); which I would imagine closes the connection before the contents of the file are read. I've removed the line.
I'm not sure about case sensitivity in content-type so I changed it to x-vcard and I changed the content-disposition to inline (a known fix for download issues with IE). Try this:
$path = "../../media/resources/";
$file = "Toni_Junas.vcf";
header('Content-Type: text/x-vcard');
header('Content-Disposition: inline; filename= "'.$file.'"');
header('Content-Length: '.filesize($path.$file));
readfile($path.$file);
Also, make sure the directory "resources" is readable (chmod 755 on the directory) and that the file exists...
put exit()
readfile($path.$file);
exit();
I'm trying to download a a word document (.docx) from the server using php. Unfortunately the document I get is corrupted. I can open the document with word, but I get these annoying messages (File is corrupted etc.). Here is my code:
$file = "documents/".$_POST["id_form"]."_document.docx";
$filename = $_POST["id_form"]."_document.docx";
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$filename);
readfile($file);
Thanks for your help!
UPDATE SOLUTION
I got the solution. I had to put ob_end_clean(); before the header and a exit; after readfile($file). Now it works fine.
Here is the working code:
$file = "documents/".$_POST["id_form"]."_document.docx";
$filename = $_POST["id_form"]."_document.docx";
ob_end_clean();
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$filename);
readfile($file);
exit;
I have an excel file that i want a user to be able to download from my server. I have looked at a lot of questions on here but i cannot find a way to correctly download the file w/o corruption. I am assuming it is the headers but i haven't had a working combination of them yet. This is what i have right now and in the corrupt file that i receive i can see the column names of the spreadsheet i want but its all messed up.
$filename = '/var/www/web1/web/public/temporary/Spreadsheet.xls';
header("Content-type: application/octet-stream");
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);
edit: Solution I forgot to add that i was using Zend and it was corrupting the files when trying to use native php methods. My finsihed code was to place a link to another action in my controller and have the files download from there
public function downloadAction(){
$file = '/var/www/web1/web/public/temporary/Spreadsheet.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="Spreadsheet.xls"');
readfile($file);
// disable the view ... and perhaps the layout
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
try doing it this way
ob_get_clean();
echo file_get_contents($filename);
ob_end_flush();
For one, only specify Content-Type once. You can use the excel-specific header but the generic application/octet-stream may be a safer bet just to get it working (the real difference will be what the browser shows the user with regards to "what would you like to open this file with", but basic browsers can rely on the extension as well)
Also, make sure you specify Content-Length and dump the size (in bytes) of the file you're outputting. The browser needs to know how big the file is and how much content it's expecting to receive (so it doesn't stop in the middle or a hiccup doesn't interrupt the file download).
So, the entire file should consist of:
<?php
$filename = '/var/www/web1/web/public/temporary/Spreadsheet.xls';
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($filename));
header("Pragma: no-cache");
header("Expires: 0");
#readfile($filename);
$file_name = "file.xlsx";
// first, get MIME information from the file
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file_name);
finfo_close($finfo);
// send header information to browser
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="download_file_name.xlsx"');
header('Content-Length: ' . filesize($file_name));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
//stream file
ob_get_clean();
echo file_get_contents($file_name);
ob_end_flush();
I am trying to download a zip file from /tmp folder in Ubuntu. However when I run the Php code it shows garbage text on the browser instead of showing a download box. I tried with a simple text file and instead of showing me a download dialog box it printed its contents on the browser. Why this force-download isn't working. Below is the code.
if (file_exists($dir.$filename)) {
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($dir.$filename));
header('Content-disposition: attachment; filename='.basename($dir.$filename));
readfile($dir.$filename);
exit(0);
}
`
Well, given any browser a MIME type "application/force-download" the browser won't know what to do with it.
Since it is a zip file, the MIME type should be "application/octet-stream" or "application/zip".
if (file_exists($dir . $filename)) {
header("Content-type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-length: " . filesize($dir . $filename));
header('Content-disposition: attachment; filename=' . basename($dir . $filename));
readfile($dir . $filename);
exit(0);
}
You cannot read whole file into memory. Change your readfile($_REQUEST['file']); into:
$handle=fopen($_REQUEST['file'], 'rb');
while (!feof($handle))
{
echo fread($handle, 8192);
flush();
}
fclose($handle);
This will read 8kb of file, then push it to the client, and so on... It will consume not much memory (as it's not reading whole file at once).
Also when forcing download always use application/octet-stream this will ensure correct sending of binary files.
im working on a content management system for that i have to download a php file using php code without executing. any one can help me on this
it is some thing like ftp. i have added the options to upload, edit and download a file. it is working fine. but while downloading a php file it is executed instead of downloading...
What i tried is:
<?php
$file = $_REQUEST['file_name'];
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));
include_once($file);
exit;
}
?>
You have to load the files content, write the content to the request and set the headers so that it's parsed as force download or octet stream.
For example:
http://server.com/download.php?name=test.php
Contents of download.php:
<?php
$filename = $_GET["name"]; //Obviously needs validation
ob_end_clean();
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". filesize($filename).";");
header("Content-disposition: attachment; filename=" . $filename);
readfile($filename);
die();
?>
This code works without any modification. Although it needs validation and some security features.
The server somehow identifies file that should be executed instead of downloaded. You have to exclude the .php file you want to download from that handling. The easiest is probably to rename the file to .php.txt.
Otherwise you should be able to configure the server to not process that particular file, or the path were it is located. How you do that depends on which server you are running.
If such php file is located on the same server/website, then just open it as normal file, e.g. $fileContents = file_get_contents($filename);
If file is on another server, you have few possible options:
1) Access it via FTP (if you have login details and access)
2) Have special URL Rewrite rule on that server which will instruct web server to send file as plain text instead of executing it (e.g. somefile.php.txt)
3) Have special script on that server and by passing file name as a parameter it will return content of that file (e.g. http://example.com/showfile.php?file=somefile.php)
This is how to download a php file instead of executing it.
Trust me it works! ..download the file php with own risk :)
<?php
function downloadThatPhp($nameOfTheFile)
{
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/text/x-vCard");
header("Content-Disposition: attachment; filename=".basename($nameOfTheFile).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($nameOfTheFile));
#readfile($nameOfTheFile);
exit(0);
}
// and this how to use:
// download that php file with your own risk :)
$file = $_REQUEST['file_name'];
$downloadThis = "http://domain-name.com/".$file;
if (file_exists($file)) {
downloadThatPhp($downloadThis);
}
?>
Hope this helps you bro :)
You can read alot about it on php.net/header, but to force a download, you can use a force-download header. This comment is amazing, check it out! :-)
if someone is looking to do this in his/her .htaccess file:
Header set Content-Disposition attachment
AddType application/octet-stream .php
or
<FilesMatch "\.(?i:php)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>