0

After much googling and tinkering around a bit i managed to export the table to .csv file but now i'd like to initiate the download via the browser instead of automatically storing in a folder.

Here's my code:

<?php
$server = 'localhost';
$user = 'root';
$pass = 'pass';
$db = 'test';

 $db = mysqli_connect($server, $user, $pass, $db);

mysqli_set_charset($db, "utf8");

$filename = 'uploads/'.strtotime('now').'.csv';

$fp = fopen($filename,"w");

$query = "SELECT id,product,status,created,summary FROM product_table";

$result =  mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());

$row = mysqli_fetch_assoc($result);

$seperator = "";
$comma = "";

foreach ($row as $name => $value){
    $seperator.= $comma. ''.str_replace('','""',$name);
    $comma=",";

}
$seperator .= "\n";

echo $seperator;

//putting the heading into the csv file
fputs($fp,$seperator);


mysqli_data_seek($result,0);

while ($row = mysqli_fetch_assoc($result)){


$seperator = "";
$comma = "";

foreach ($row as $name => $value){

    if (strcmp($name,'summary') == 0){


            $value = str_replace( array( "\r" , "\n", "\r\n", "\n\r" ) ,'' , $value);
            $value = str_replace('</b><br>','',$value);
            $value = str_replace('<b>','',$value);
            $value = str_replace('<br>','',$value);
            $value = str_replace('<br />','',$value);
    }

    $seperator.= $comma. ''.str_replace('','""',$value);
    $comma=",";

    }
$seperator .= "\n";


//putting the heading into the csv file
fputs($fp,$seperator);


}

fclose($fp);



?>

Running the above script stores the .csv file in the folder uploads. What changes to do i need to make to enable browser download so that users can run the script and download the .csv file via the browser? thanks!

EDIT: Furthermore what modifications would i have to make if wanted to disable saving to the folder and only enable user/browser downloads? thanks.

4
  • You seem to have forgotten to add tag homework. Commented Dec 9, 2012 at 9:58
  • @Christian: Despite not mentioning it i did try different methods of getting it to work but failed. On another note, this is a work project with a deadline. I needed to get what had to be done and then go figure out the why. Commented Dec 10, 2012 at 0:45
  • In my opinion, I'd reject work which I don't know how to carry out in the first place... Commented Dec 10, 2012 at 7:44
  • @Christian: Well in my opinion, i consider it a challenge but lets agree to disagree and leave it there. Commented Dec 11, 2012 at 3:52

3 Answers 3

4

Try with this:

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=your_file.csv');
header('Pragma: no-cache');
readfile($filename);
Sign up to request clarification or add additional context in comments.

9 Comments

great! that seems to work. However it appears to add the table titles twice - any idea why? thanks.
strange, don't think that this has anything to do with the download. Are you sure that the original $filename is looking as it should?
well yeah considering the data is there.?Its just the title that gets added twice..
Muller: btw it doesn't have the extra title line in the .csv file in the uploads directory, only in the browser downloaded version.
Sorry, has to be something else. Try with another browser? This is the standard approach of download csv files that I have been using for years without problems, this issue has to be something else.
|
1

surely the table titles appear twice because this line appears twice in the code in different places:

//putting the heading into the csv file
fputs($fp,$seperator);

Comments

0

i think following two lines enough to force browser to auto download csv file

header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="dishes.csv"');

where filename -- give name name csv file name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.