Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:46:39+00:00 2026-06-15T15:46:39+00:00

After much googling and tinkering around a bit i managed to export the table

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:46:40+00:00Added an answer on June 15, 2026 at 3:46 pm

    Try with this:

    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename=your_file.csv');
    header('Pragma: no-cache');
    readfile($filename);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know much about Emacs, but after some googling, I edited my .emacs
There are many similar questions, but nothing that answers this specifically after googling around
This is probably jquery basics, but I can't find a solution after much googling.
After a bit of googling I'm not seeing this turn up much. Is there
After much googling and console testing I need some help with arrays in rails.
After much searching and googling I am coming back to the well. I have
After much fiddling, I've managed to install the right ODBC driver and have successfully
I know about $(Delphi) and $EDNAME but after much seaching I cant find a
After much googling, stackoverflowing (?) and blog-reading I still cant completely solve the issue.
After a fair bit of Googling I have not found any authoritative, conclusive information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.