CSV is not working PHP

1k Views Asked by At

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 -->                                                            
2

There are 2 best solutions below

2
On

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.

0
On

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