when i send a csv file to the browser using header('Content-Disposition: attachment filename="xyz.cs" '); the file named as export.php (filename of the php-script) instead of xyz.csv
public function sendCSV($arrBody, $arrHeadings)
{
$fh = fopen('php://output', 'w');
ob_start();
fputcsv($fh, $arrHeadings);
if (! empty($arrBody)) {
foreach ($arrBody as $line) {
fputcsv($fh, $line);
}
}
$strBuffer = ob_get_clean();
$strFile = 'csv_' . date('Ymd') . '-' . uniqid() . '_export.csv';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment filename=\"$strFile\";" );
header("Content-Transfer-Encoding: binary");
exit($strBuffer);
}
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 try to force download a pdf file from php that are in server...but all the file that i download only 1kb size.It not the same with actual size do i need to declare file size before download?
<?php
$path = "C:\Users\omamu02\Desktop\TESTPRINT" ;
$file = "NMT PRV PHG 370 2017.pdf";
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/force-download");//Get and show report format
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();
error_reporting(0);
?>
First of all, you should fix your filename= $file header. Wrap your $file variable with a ' chars at least. Also, you don't need the closing tag in the end of the PHP file.
And I'm not sure about your headers, so I suggest you try the function below, it's quite common for any type of data and already contains some bugfixes and workarounds:
function download_file($file_path, $file_name = null, $file_type = 'application/octet-stream')
{
if ($file_name === null)
{
$file_name = basename($file_path);
}
if (file_exists($file_path))
{
#set_time_limit(0);
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_type);
header('Content-Disposition: attachment; filename="' . str_replace('"', "'", $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: ' . filesize($file_path));
readfile($file_path);
}
exit;
}
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;