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");
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?
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);
}
I had created a website using php. In that website,I want to create a report like thing,which involves transferring details from one table to an excel sheet which is downloadable.Is there any way to do this work? Please help me to find the solution.
Thank you.
You can fetch all rows from the database and use fputcsv function to put it as csv
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=export.csv");
header("Content-Transfer-Encoding: binary");
$csvHandler = fopen("php://output", 'w');
while ($row = mysql_fetch_array ($res))
{
fputcsv($csvHandler, $row);
}
You can use PHPExcel for generating data
and a simple function for file download:
function show_download($data, $name, $type = "unknown/unknown") {
header('Content-Type: ' . $type);
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Content-Length: ' . strlen($data));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
print $data;
exit();
}
You can create a script that exports a CSV file like this :
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=clients_" . date("Ymd") . ".csv");
//Open PHP output stream
$fd = fopen('php://output', 'w');
$tab = array();
//get your results from your database
foreach ($results as $res)
$tab[] = array($res->Client_Name, $res->Client_Email);
foreach ($tab as $val)
fputcsv($fd, $val);
fclose($fd);
am integrating force download using php. when i download the flv files and open in video players its shows error.
$Filename = '/* file name */';
header('Content-Description: File Transfer');
header("Content-type: video/flv");
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename=test.flv' );
header("Pragma: no-cache");
header('Content-length: '.filesize($Filename));
$hFPi = fopen ("$Filename", "rb");
while (!feof($hFPi))
{
$sBuf = fread ($hFPi, filesize($Filename) );
echo $sBuf;
}
fclose ($hFPi);
Please help..
$Filename = '/* file name */';
$path = "../../upload/";
$file = $path.$filename;
savedata1();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/flv");
header("Content-Transfer-Encoding: binary");
readfile($file);
this will help u. Try it in ur code and u will find ur answer...
This code working great to download mp4, flv, mpeg file from my end.
Here:
$file_name = The Downloaded video file name,
$file_path = Base Path of the original file in the server.
header('Pragma: public'); // required
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: video/mpeg");
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
ob_clean();
flush();
readfile($file_path);
exit;
Try
$Filename = 'test.flv';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: video/flv");
header("Content-Disposition: attachment; filename=".basename($filename).";" );
header("Content-Transfer-Encoding: binary");
readfile("$Filename ");
This is what I do to force a download of a file (for me it's ZIP but it should work for any file type though). Keep in mind you'll have to tweak it as I store my db information differently than you probably do.
header("Content Description: File Transfer");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must revalidate");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename(".".$filepath)."");
header("Content-Length: " . filesize(".".$filepath));
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile(".".$filepath);
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;