I have this simple code
<?php
//header info for browser
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=file.xls");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: 0");
echo pack("CCC",0xef,0xbb,0xbf);//utf-8
/* * *****Start of Formatting for Excel****** */
$data = "text1 \t text2 \t";
print($data);
?>
That creates an excel file. My problem is that the \t tab character is not working. Everything is in the first column of the excel file. What is wrong?
I'm use "\r\n" escs not so long:
$data = "text1 \\t text2 \\t";
echo '<pre>';
var_dump($data);
echo '</pre>';
If anyone has the same problem, I found a solution
<?php
//header info for browser
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=file.xls");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: 0");
$data = "text1 \t text2 \t";
$data = mb_convert_encoding($data, 'UTF-16LE', 'UTF-8');
$data = "\xFF\xFE" . $data;
echo $data;
?>
Related
There's this code which I have
function pushfile(&$file) {
header_remove();
if (isset($file["type"])) header('Content-Type: '.$file["type"]);
if (isset($file["name"])) header('Content-Disposition: attachment; filename="'.$file["name"].'"');
print $file["name"];
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: ");
echo $file["content"];
exit;
$h=fopen($filename,"r");
$content = fread($h, filesize($filename));
fclose($h);
$file["type"] = "application/"; //
$file["name"] = $real_filename;
$file["content"] = $content."*".$context['user']['id'];
pushfile($file);
This ID number was well placed into the downloaded file, but it disappeared after converting it (e.g. from pdf to epub). Is there a good solution for that?
I am trying to export my post data into csv/xls but both return '???' instead of arabic string. I have set headers...
//output the headers for the XLS file
header('Content-Encoding: UTF-8');
header("Content-Type: Application/vnd.ms-excel; charset=utf-8");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-Disposition: Attachment; Filename=\"$filename\"");
header("Expires: 0");
header("Pragma: public");
Does it work if you add this to the end of the header output?
print chr(255) . chr(254) . mb_convert_encoding($csv_output, 'UTF-16LE', 'UTF-8');
Found it here and here .
I want to read an excel file with php and send its content to the client. What I really want to do is by changing the content type and other html headers force browser to download the file instead of showing its content. Everything is ok and user can download the file but when he tries to open it contents are not displayed in right format and shape. The thing which comes to my mind is that I should not send response in unicode string and have to do it in binary format which excel can know it. This is my current code :
$filename = $_GET['file'];
$ext = $_GET['type'];
$filename .= '.' . $ext;
$path = "../generatedReports/" . $filename;
header('Content-type: application/vnd.ms-excel;');
header('Content-Disposition: attachment; filename=' . $filename);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path));
$size = filesize($path);
$f = fopen($path, 'r');
$content = fread($f, $size);
echo $content;
Any solution?
Please try with the below code,
echo unicode2utf8(hexdec("00CE")); // Result: Î
// Or the function that will recognize U+ in front of string, and will skip it to show the character
function unicodeCodePointToChar($str) {
if (substr($str,0,2) != "U+") return $str;
$str = substr($str,2); // Skip U+
return unicode_to_utf8(array(hexdec($str)));
}
echo unicodeCodePointToChar("U+00CE"); // Result: Î
my problem was solved by this answer. I've changed my code to this :
$filename = $_GET['file'];
$ext = $_GET['type'];
$filename .= '.' . $ext;
$path = "../generatedReports/" . $filename;
header('Content-type: application/vnd.ms-excel;');
header('Content-Disposition: attachment; filename=' . $filename);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");
ob_clean(); // discard any data in the output buffer (if possible)
flush(); // flush headers (if pos
readfile($path);
and everything got nice.
I've written a function to download a file in csv format in php which goes as follows:-
function report_download_csv($fields, $data, $filename) {
global $CFG;
require_once($CFG->dirroot.'/user/profile/lib.php');
$filename = clean_filename($filename.'-'.date('Y-m-d').'.csv');
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
$row = array();
$date_field = array('registrationdate', 'expirydate', 'startdate', 'enddate');
$totalrow = count($fields);
$row[] = implode(',',$fields)."\n";
foreach($data as $d){
for($i=0;$i<$totalrow;$i++){
$row_d[] = $d->$fields[$i];
}
$row[] = implode(',',$row_d);
$row_d = array();
}
echo implode($row, "\n");
die;
}
However, when I call the function, it simply prints the result in the browser. Is there something I should change in the function?
Try like this..
header('Content-Type: application/csv');
header('Pragma: no-cache');
The following headers should force a download
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/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
I have Excel spreadsheets stored in a MySQL table longblob field. I need to retrieve this data and then stream it to the user as a downloadable file, preferably without writing it to disk first.
Possible?
Edit - Eh, just figured it out... Posted in answer below.
function getfile($blockid)
{
global $msa_db;
$sql = "select filename, filedata from blocks where blockid = '$blockid'";
$query = mysql_query($sql, $msa_db);
$result['filename'] = mysql_result($query,0,0);
$result['filedata'] = mysql_result($query,0,1);
return $result;
}
function download($fileinfo)
{
$file = base64_decode($fileinfo['filedata']);
header("Cache-Control: no-cache private");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.$fileinfo['filename']);
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($file));
echo $file;
exit;
}
$fileinfo = getfile($blockid);
download($fileinfo);
Just got the solution
http://www.codeproject.com/Articles/831185/CREATE-DOWNLOAD-ZIP-FILE-USING-PHP
$data = base64_decode($data);
header("Cache-Control: no-cache private");
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename='".$identifier.".zip'");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($data));
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
flush();
echo $data;