CSV file is not downloading using codeigniter - php

I am trying to export few data in CSV file. Though the file is successfully downloaded as well as stored in particular path. If I opened downloaded file from the browser, file showing empty results I don't know where I made mistake please help me out
My Controller
public function export_webdata($p_id,$inptid){
$project = $this->Lead_Model->get_project_single($p_id);
$filename = $project[0]->p_name.'_WebsiteInfo_'.time().'.csv';
$path = getcwd().'/public/files/'.$project[0]->p_name.'/';
if (!file_exists($path)){
mkdir($path, 0777, true);
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$output = fopen($path.$filename, 'w');
$header = array("Company","Country","Website");
fputcsv($output, $header);
$extdata = $this->Lead_Model->webdata_export($inptid);
foreach ($extdata as $key=>$line){
fputcsv($output,$line);
}
fclose($output);
}
My Model
public function webdata_export($inptid)
{
$response = array();
$db5 = $this->load->database('output_db', TRUE);
$q = $db5->select('Company,Country,Website')->get_where('website_info', ['input_id' => $inptid, 'type_id' => 2,'uploaded_by' => $this->session->admin_id]);
$response = $q->result_array();
return $response;
}

you need to add below code to clean buffer before or after header
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
while ( ob_get_level() ) {
ob_end_clean();
}

Related

export sql to csv php codeigniter

Could someone help me with this problem?
I have following function
public function exportcsv(){
//echo 'hi'; exit();
// file name
$filename = 'blocked_users_'.date('Y-m-d').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
// get data
$user_data = $this->Common_model->get_users_for_csv();
// file creation
$file = fopen($filename, 'w');
$header = array("full_name", "phone", "email", "payment_method", "delivery_address", "delivery_city", "timestamp");
fputcsv($file, $header);
foreach ($user_data as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
File it's generated 2 times, one, on server root second it's downloaded to disk, but on server root file it's ok with headers and content, the second one which is downloaded on disk it's empty.

Create excel file without library

I using this code:
header( "Content-Type: application/vnd.ms-excel" );
header("Content-Disposition: attachment; filename=liste.xls");
echo "\xEF\xBB\xBF";
and I writing my SQL datas in a while loop like this:
echo "\n<table><tr><td>".$first[$post_a]."</td>\t<td>".$second[$post_a]."</td>\t<td>".$third[$post_a]."</td>\t</td></tr></table>";
When I try to open this created file, excel saying version and type are unmating even so I can open file after this warning on desktop but when I try to open on my phone file can't opening after warning.
You need to something like this here. Also ignore my logic in for-loop, may want to use your own.
$filename = "export_".date("m-d-Y_hia") . ".csv";
ob_end_clean();
$fp = fopen($filename,"w");
$is_header = true;
$no_meta_appnd = array('SKU','Name','Publish','Categories','Description');
foreach ($res as $index => $product){
$arr = array();
if($is_header){ // put all the header in array
foreach ($product as $index => $p){
if(in_array($index,$no_meta_appnd)){
array_push($arr,$index);
}else{
array_push($arr,"Meta: ".$index);
}
}
$is_header = false;
fputcsv($fp,$arr); // add headers
$arr = array(); // empty out array for first row
foreach ($product as $index => $p){
array_push($arr,$p);
}
fputcsv($fp,$arr); // add first row after header
}else{
foreach ($product as $index => $p){
array_push($arr,$p);
}
fputcsv($fp,$arr); // rest of the rows
}
}
//unserialize();
fclose($fp);
// download
// header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");
header('Pragma: no-cache');
readfile($filename);
// deleting file
unlink($filename);
exit();

Provide Download path for files in Codeigniter

So The thing in a table there are some checkboxes so based on the checkboxes user have checked it will downnload a particular coloumn of the table in the .php format. So I have tried the following code:-
public function export_php() {
$export = $this->session->userdata('export_id');
$exports = $this->query_revision_model->export($export);
$data = "";
foreach ($exports as $export) {
$data .= $export->sql_changes . "\r\n";
}
$filename = "export.php";
$handle = fopen($filename, w);
fwrite($handle, $data);
fclose($handle);
}
So,Everything is working fine but I'm unable to provide the path for the download files,So it's saving on the application folder which I don't want.
I tried force_download($filename,$data) did n't work because I am using ajax.
You need to force the browser to download file except display.
public function export_php() {
$export = $this->session->userdata('export_id');
$exports = $this->query_revision_model->export($export);
$data = "";
foreach ($exports as $export) {
$data .= $export->sql_changes . "\r\n";
}
$filename = "export.php";
$handle = fopen($filename, w);
fwrite($handle, $data);
fclose($handle);
$filePath = APPPATH.$fileName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/php");
// Read the file
readfile($filePath);
exit;
}
and in your ajax code try this instead of ajax
window.location.replace('Your URL Here')
Hope it helps!

Wordpress export csv to local /wp-content/uploads

I want to put csv file under /wp-content/uploads instead of downloading it . Following php code download this csv file
I am using this php code
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=data.csv');
$stream = fopen('php://output', 'w+');
fputcsv($stream, $csv_header);
foreach($arrCSV as $key => $assoc_row){
$numeric_row = array();
foreach ($csv_header as $header_name) {
mb_convert_variables('UTF-8', $assoc_row[$header_name]);
$numeric_row[] = $assoc_row[$header_name];
}
fputcsv($stream, $numeric_row);

php://output results in empty file

I have a Wordpress theme template file in use by a page. The template queries the db for an array and then attempts to output the result to a csv file. No output to the browser is expected. Here is code:
/***
* Output an array to a CSV file
*/
function download_csv_results($results, $name = NULL)
{
if( ! $name)
{
$name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv';
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "w");
foreach($results as $result)
{
fputcsv($outstream, $result);
}
fclose($outstream);
}
The file is written to the user's downloads directory as expected, but it is empty. I've debugged to verify there is a result of 117 elements. The loop above is executing. It's as though the output buffer is not being flushed or is being cleared by Wordpress.
Could you try to use :
$outstream = fopen("php://output", "a");
instead of :
$outstream = fopen("php://output", "w");

Categories