Jump to content

Recommended Posts

Hey guys,

 

I have created CSV export, but I have problems when downloading the file. When I store it to a folder everything is OK. But when i use headers and force my browser to download the file and ofers me open or save, my CSV file is empty. I can't figure out the problem?

 

Any ideas...

 

<?php 
include ("../admin/servis/include/session.php");


$id_kalkulacije = $_GET["id"];


// create a file pointer connected to the output stream
$output = fopen('export/export.csv', 'w');


// output the column headings
fputcsv($output, array('Kataloski broj', 'VPC', 'MPC', 'Preporucena VPC'), ";");


// fetch the data
$upit = "SELECT kataloski_broj, neto_VPC, neto_MPC, preporucena_VPC FROM kalkulacija_stavke WHERE id_kalkulacija = '$id_kalkulacije'";
$rows = mysql_query($upit) or die (mysql_error());


//funkcija za zamjenu . u ,
$find = '.';
$replace = ',';


// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) {

$text = str_replace($find, $replace, $row);


fputcsv($output, $text, ";");


}


// output headers so that the file is downloaded rather than displayed
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="export.csv"'); 
exit;


header("Location:index_kalkulacija.php");
exit;
?>

Link to comment
https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/
Share on other sites

Isn't this part below supposed to open the dialog box asking if I want to download or open file?

 

// output headers so that the file is downloaded rather than displayed
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="export.csv"'); 
exit;

Ok, I did this, but now when I open CSV file I have only word array inside. What am I missing???

 

// output headers so that the file is downloaded rather than displayed
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="export.csv"'); 
echo $text;
exit;

You can't just echo an array, as you have seen.

You could try using implode, and/or foreach.

 

Jessica, I've got this, but the message I get is unexpected T_LIST. I checked the syntax and it's OK. Do you see anything wrong with it?

 

while ($row = mysql_fetch_assoc($rows)) {

$text = str_replace($find, $replace, $row);


fputcsv($output, $text, ";");


}
foreach ($text as list($a, $b, $c, $d)) {

$comma_separated = implode(";", $text);

}
// output headers so that the file is downloaded rather than displayed
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="export.csv"'); 
echo $comma_separated;
exit;

OK, now I'm totally lost! can someone tell me how can I open download dialog after the CSV file is created and download the file to location user wants?

 

<?php 
include ("../admin/servis/include/session.php");


$id_kalkulacije = $_GET["id"];


// create a file pointer connected to the output stream
$output = fopen('export/export.csv', 'w');


// output the column headings
fputcsv($output, array('Kataloski broj', 'VPC', 'MPC', 'Preporucena VPC'), ";");


// fetch the data
$upit = "SELECT kataloski_broj, neto_VPC, neto_MPC, preporucena_VPC FROM kalkulacija_stavke WHERE id_kalkulacija = '$id_kalkulacije'";
$rows = mysql_query($upit) or die (mysql_error());


//funkcija za zamjenu . u ,
$find = '.';
$replace = ',';


// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) {

$text = str_replace($find, $replace, $row);


fputcsv($output, $text, ";");


}


header("Location:index_kalkulacija.php");
exit;
?>

This is how I do it, using STDOUT as the output file so I can use fputcsv

 

//
// sample usage
//
$sql = "SELECT * FROM employees";

sql2csv ($mysqli, $sql, 'employees.csv', 1);

//
// CSV download function
//
function sql2csv($mysqli, $sql, $filename='', $headings=1)
{
if (!$filename)
 $f = 'download_' . date('ymdhi') . '.csv';
else
	 $f = $filename;
$fp = fopen('php://output', 'w');	 // so you can fputcsv to STDOUT
if ($fp) {
 $res = $mysqli->query($sql);
 if ($res) {
	 header('Content-Type: text/csv');
	 header('Content-Disposition: attachment; filename="'.$f.'"');
	 header('Pragma: no-cache');
	 header('Expires: 0');
	 $row = $res->fetch_assoc();
	 if ($headings) {
		 fputcsv($fp, array_keys($row));
	 }
	 do {
		 fputcsv($fp, $row);
	 } while ($row = $res->fetch_assoc());

 }
 else echo "Error in query";
 fclose($fp);
 exit;
}

}

 

edit - added mysqli parameter

Edited by Barand

Right now you are writting out your CSV data to a file.  Rather than doing that you want to get that data as a string so that you can echo it out to the browser.  One convinent way to do that is to use a temp file which you can open using the function tmpfile.  That lets you still use fputcsv to generate the CSV contents easily w/o having to create a static file which could cause issues if two people tried to do a csv export at the same time.

 

// create a file pointer connected to the output stream
$output = tmpfile();
$len = 0; //Size of the csv data for use in a content-length header

// output the column headings
$len += fputcsv($output, array('Kataloski broj', 'VPC', 'MPC', 'Preporucena VPC'), ";");

// fetch the data
$upit = "SELECT kataloski_broj, neto_VPC, neto_MPC, preporucena_VPC FROM kalkulacija_stavke WHERE id_kalkulacija = '$id_kalkulacije'";
$rows = mysql_query($upit) or die (mysql_error());

//funkcija za zamjenu . u ,
$find = '.';
$replace = ',';

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) {
   $text = str_replace($find, $replace, $row);
   $len += fputcsv($output, $text, ";");
}

// output headers so that the file is downloaded rather than displayed
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="export.csv"'); 
header('Content-length: '.$len);

//Output the generated csv data
rewind($output);
fpassthru($output);
fclose($output);
exit;

 

 

kicken,

 

You're solution worked like a charm. I realise now what my problem was. I made the whole prinicpal wrong from the begining.

 

Thanks for you're help!!!

 

Also, thanks to all other people who replied to my scream for help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.