Welcome Guest, Not a member yet? Register   Sign In
force_download problem
#1

[eluser]Mhabub[/eluser]
Dear All,
In my application I create CSV file runtime and give option to download the the CSV file using force_download().
But while downloading it add the blank line at the top of the CSV file. The original file it does not have blank line.

Before download;
Date,"Granular Totes","Granular Bags","Pellet Totes","Pellet Bags","Total Tons",Manhours,Tons/Manhour
2010-04-04,2,1,1,1,5,2,2.5
2010-04-04,10,10,10,10,40,10,4

After download:



Date,"Granular Totes","Granular Bags","Pellet Totes","Pellet Bags","Total Tons",Manhours,Tons/Manhour
2010-04-04,2,1,1,1,5,2,2.5
2010-04-04,10,10,10,10,40,10,4


Thanks in advance for any kind of help.
#2

[eluser]Mischievous[/eluser]
Need some code to see... otherwise can't really help ya out.
#3

[eluser]Mhabub[/eluser]
Here is the code:

$file_path = 'csv_data/';
$csv_file_name = 'ogsi_'.date("Y-m-d_h-i-m").'.csv';

//chmod($file_path."/".$csv_file_name, 0777);

$fp = fopen($file_path.$csv_file_name, 'w');

$result_array = array();
$result_array[0] = "Date,Granular Totes,Granular Bags,Pellet Totes,Pellet Bags,Total Tons,Manhours,Tons/Manhour";
$i = 1;
foreach ($csv_record->result() as $rec){
$result_array[$i]=$rec->creation_date.','.$rec->granular_totes.','.$rec->granular_gags.','.$rec->pellet_totes.','.$rec->pellet_bags.','.$rec->total_tons.','.$rec->manhours.','.$rec->tons_by_manhour;
$i++;
}

foreach($result_array as $line_data){
fputcsv($fp, split(',', $line_data));
}
fclose($fp);


$this->load->helper('download');
$doc_file_name = $csv_file_name;
$doc_file_loc = file_get_contents($this->baseurl.'csv_data/'.$csv_file_name); // Read the file's contents
force_download($doc_file_name,$doc_file_loc);
#4

[eluser]Mischievous[/eluser]
Not sure, seems like it would be a problem with something after its written. But anyways your code could be cleaned up a bit
Code:
<?php

$file_path = 'csv_data/';
$csv_file_name = 'ogsi_' . date("Y-m-d_h-i-m") . '.csv';
//chmod($file_path.”/”.$csv_file_name, 0777);

$fp = fopen($file_path . $csv_file_name, 'w');
$result_array =  array();
$result_array[] = 'Date,Granular Totes,Granular Bags,Pellet Totes,Pellet Bags,Total Tons,Manhours,Tons/Manhou';

foreach($csv_record->result() as $rec)
{
$result_array[] = $rec->creation_date.','.$rec->granular_totes.','.$rec->granular_gags.','.$rec->pellet_totes.','.$rec->pellet_bags.','.$rec->total_tons.','.$rec->manhours.','.$rec->tons_by_manhour;
}

foreach($result_array as $line_data)
{
    fputcsv($fp, split(',', $line_data));
}

fclose($fp);
$this->load->helper(‘download');
$doc_file_name = $csv_file_name;
$doc_file_loc =  file_get_contents($this->baseurl.‘csv_data/'.$csv_file_name);
force_download($doc_file_name,$doc_file_loc);
            
?>

You don't really need to specify index and run $i ... just add another with "[]"
#5

[eluser]Mhabub[/eluser]
Summer Student,
thanks. let me try with this
#6

[eluser]Mhabub[/eluser]
till now not working!!




Theme © iAndrew 2016 - Forum software by © MyBB