0

I have a data in this form:

526,8696,Peach,Del Monte,Breakout Damage,TT Damage,Agricom,Mango,Damage to contents of cartons,4,2,P,2014-12-08,748,Atlanta 404,2014-12-08,Amir

Here is the code to create CSV:

$receiveQuery  = $objadminViewFunctions->exportFile();
$fp = fopen('data.csv', 'w');

$column = 'Serial No.,Report Number,Pallet Id,Type,Consignee,Damage,Damage Description,Label,Vessel,Location,Suryeyor';

fputcsv($fp, split(',',$column));

while($rows = $receiveQuery->fetch_assoc())
{
    $data = $rows['report_number'].','.$rows['pallet_ID'].','.$rows['type'].','.$rows['consignee'].','.$rows['damage'].','.$rows['damage_desc'].','.$rows['label'].','.$rows['variety'].','.$rows['category_code'].','.$rows['pieces'].','.$rows['hold'].','.$rows['deck'].','.$rows['dDate'].','.$rows['vessel'].','.$rows['location'].','.$rows['dDate'].','.$rows['suryeyor'];
    fputcsv($fp, split(',',$data));
}

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache"); 
header("Expires: 0");

The problem is that it is creating CSV file but with the HTML of my page rather than the data I want to export from that file.

Here is what I got in the CSV file when I press export button.

 <!DOCTYPE html>                                                                
<html class="no-js">                                                                
<head>                                                              
<title>Reports</title>                                                              
<link href="../assets/styles/admin/vendors/bootstrap-wysihtml5/src/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">                                                                
<link href="../assets/styles/admin/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">                                                                
<link href="../assets/styles/admin/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">                                                             
<link href="../assets/styles/admin/vendors/easypiechart/jquery.easy-pie-chart.css" rel="stylesheet" media="screen">                                                             
<link href="../assets/styles/admin/vendors/datepicker.css" rel="stylesheet" media="screen">                                                             
<link href="../assets/styles/admin/assets/styles.css" rel="stylesheet" media="screen">                                                              
<link rel="stylesheet" href="../assets/styles/admin/assets/validationEngine.jquery.css" type="text/css">                                                                
<!-- HTML5 shim  for IE6-8 support of HTML5 elements -->                                                            
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->                                                             
<!-- HTML5 shim  for IE6-8 support of HTML5 elements -->                                                            
Keep Coding
  • 636
  • 9
  • 26
  • 2
    This question has not enough details to give a good answer. Are you sing a framework ? Which one ? – Lorenz Meyer Dec 12 '14 at 08:01
  • I am using custom PHP to get this task done. Let me explain the question again. – Keep Coding Dec 12 '14 at 08:02
  • My code does create CSV file but instead of creating csv file it simply create CSV file with the page HTML content in it rather than my data. – Keep Coding Dec 12 '14 at 08:05
  • I have made sure and it do not have any html data in it but still it outputs the html data in it. dont know why. – Keep Coding Dec 12 '14 at 08:10
  • Why are you first concatenating your $data and then splitting it? LOL – Klemen Tusar Dec 12 '14 at 08:11
  • I have copied code from somewhere that was working there. So I have used this that way. Can you please make adjustments? – Keep Coding Dec 12 '14 at 08:13
  • Your custom page is clearly outputting headers before it ever gets to the custom headers you defined for the csv. – Jason Roman Dec 12 '14 at 08:14
  • Really? Fix a copy & pasta? Jeeeebus.... Just use the $rows array instead of concatenating and splitting your $data. Otherwise you might wanna clear the output buffer before calling the new header and printing your CSV. – Klemen Tusar Dec 12 '14 at 08:14
  • How can I do this Sir? I mean can you post answer for me? – Keep Coding Dec 12 '14 at 08:16
  • @JasonRoman how that can be solved then? any fix? – Keep Coding Dec 12 '14 at 08:17
  • http://code.stephenmorley.org/php/creating-downloadable-csv-files/ – Klemen Tusar Dec 12 '14 at 08:17
  • 1
    Please, don't copy and paste and let us fix it. Go try something, run into error, and then ask here. – Chilion Dec 12 '14 at 08:17
  • @Chilion what if the code is working on my other project but not here? That is the problem. – Keep Coding Dec 12 '14 at 08:19
  • So, what is the output of: $objadminViewFunctions->exportFile(); ? – Chilion Dec 12 '14 at 08:20
  • Here is the Output: Array ( [0] => Array ( [0] => 1 [de_ID] => 1 [1] => 1 [report_ID] => 1 [2] => 4788 [pallet_ID] => 4788 [3] => Apple [type] => Apple [4] => Dandrea [consignee] => Dandrea [5] => Stow Damage [damage] => Stow Damage [6] => TT Damage [damage_desc] => TT Damage [7] => Valdovinos [label] => Valdovinos [8] => Flame Seedless [variety] => Flame Seedless [9] => Empty and/or Missing Cartons [category_code] => Empty and/or Missing Cartons [10] => 2 [pieces] => 2 [11] => 2 [hold] => 2 [12] => C [deck] => C [13] => 2014-12-08 ) ) – Keep Coding Dec 12 '14 at 08:24

2 Answers2

0

This is a piece of my working CSV export function:

public function exportAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        // output headers so that the file is downloaded rather than displayed
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=products.csv');

        // create a file pointer connected to the output stream
        $output = fopen('php://output', 'w');

        // output the column headings
        fputcsv($output, array('Product', 'Name', 'Price', 'Description'));

        // fetch the data
        $product_info = $this->product->getAll();
        foreach ($product_info as $key => $product) {
            $row['product'] = $product->product_id;
            $row['name'] = $product->name;
            $row['price'] = $product->price;
            $row['description'] = $product->description;

            fputcsv($output, $row);
        }
    }

Make sure to output the headers first, so that it won't load the HTML.

Matheno
  • 4,112
  • 6
  • 36
  • 53
0

You haven't let user to actually download the csv file you made but simply change the content type of current output to Content-type: text/csv. You created a data.csv but you didn't send it to user.

Here is something might work:

header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache"); 
header('Expires: 0');
header('Content-Length: ' . filesize('data.csv'));
readfile('data.csv'); // This actually out puts your csv file
exit; // This is importent that stops the current script and prevents the out put of your html part
kyo
  • 645
  • 4
  • 7