budimir Posted February 11, 2013 Share Posted February 11, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/ Share on other sites More sharing options...
Christian F. Posted February 11, 2013 Share Posted February 11, 2013 I don't see where you're actually sending the CSV data to the browser..? Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411811 Share on other sites More sharing options...
budimir Posted February 11, 2013 Author Share Posted February 11, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411814 Share on other sites More sharing options...
Christian F. Posted February 11, 2013 Share Posted February 11, 2013 That just sends the headers that identifies the content as a CSV, but it does not send the actual content. You need to send/echo that out manually, between the exit and the last header () call. Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411815 Share on other sites More sharing options...
budimir Posted February 11, 2013 Author Share Posted February 11, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411817 Share on other sites More sharing options...
Jessica Posted February 11, 2013 Share Posted February 11, 2013 You can't just echo an array, as you have seen. You could try using implode, and/or foreach. Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411822 Share on other sites More sharing options...
budimir Posted February 11, 2013 Author Share Posted February 11, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411825 Share on other sites More sharing options...
budimir Posted February 11, 2013 Author Share Posted February 11, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411829 Share on other sites More sharing options...
Barand Posted February 11, 2013 Share Posted February 11, 2013 (edited) 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 February 11, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411831 Share on other sites More sharing options...
kicken Posted February 11, 2013 Share Posted February 11, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411833 Share on other sites More sharing options...
budimir Posted February 12, 2013 Author Share Posted February 12, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274353-csv-download-after-created-problem/#findComment-1411980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.