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 7992665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:37:56+00:00 2026-06-04T13:37:56+00:00

I have done a script in PHP to export a MySQL table into a

  • 0

I have done a script in PHP to export a MySQL table into a CVS file when clicking a button. It’s working fine but now I need to export 1 table per CSV file, all this in a ZIP (or RAR) file when clicking a button.

Basically it should be:
Export table:

mytable.csv

Export all tables:

export.ZIP containing:

–mytable1.csv

–mytable2.csv

–mytable3.csv

I could loop the export function for each table but that will not put everyting into the ZIP:

/* EXPORT ALL TABLES */
if (isset($_POST['export_all_tables'])){
    $Qlist_table = $pdo->prepare('SHOW TABLES');
    $Qlist_table->execute(array(''));
    foreach ($Qlist_table as $Alist_table)
      export_table($Alist_table[0]);
}


 function export_table($table_to_export){
$Qselect = $pdo->prepare('SELECT * FROM '.$table_to_export.''); 
    $Qselect->execute(array(''));
    $results = $Qselect->fetchAll(PDO::FETCH_ASSOC);
...
...
...

    header("Content-disposition: attachment; filename=".$fileName);
    header("Content-Type: application/force-download");
    header("Content-Transfer-Encoding: application/vnd.ms-excel\n");
    header("Pragma: no-cache");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
    header("Expires: 0");

    echo $outputCsv;
    exit();
}

I don’t think you need all the code of the export function, if yes let me know.

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-04T13:37:58+00:00Added an answer on June 4, 2026 at 1:37 pm

    Actually I found the solution thanks to addFile(). Here is the code:

    if (isset($_POST['export_all_tables'])){
        // Name of the file to export
        $date = date('Y_m_j_H\hi');
        $fileName = 'Export_all_'.$date.'.zip';
    
        // Headers to create the ZIP file
        header("Content-disposition: attachment; filename=".$fileName);
        header("Content-Type: application/force-download");
        header("Content-Transfer-Encoding: application/zip;\n");
        header("Pragma: no-cache");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
        header("Expires: 0");
    
        // We create the ZIP file       
        $zip = new ZipArchive;
        $result_zip = $zip->open($fileName, ZipArchive::CREATE);    // We open the file
        if ($result_zip === TRUE) {
    
            $Qlist_table = $pdo->prepare('SHOW TABLES');
            $Qlist_table->execute(array(''));
    
            // For each table
            foreach ($Qlist_table as $Alist_table) {
    
                    $content = export_table($Alist_table[0]);
                    $fileName = $Alist_table[0].'_'.$date.'.csv';   
                    $zip->addFile($fileName, $fileName);            // We add the CSV file from the server
            }
            $zip->close();
        }
        else {
            echo 'Failed, code:' . $result_zip;
        }   
        readfile("Export_all.zip");             // To download the ZIP file
        exit(); 
    }
    

    and the exporttable function ends like that to write the file on the server:

    $fh = fopen($fileName, 'w') or die("Can't open file");
    $stringData = $outputCsv;   // outputCsv is the content of the table
    fwrite($fh, $stringData);
    fclose($fh);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that produces a .CSV export file from a MySQL
I have done a a bash script which run php script. It works fine
I have created a php script to import rss feed into the database. The
i have a script that calls a php file and gets an JSON object
I have a php script that reads an XML file and modifies it with
I have done a simple GoogleMap which responses to script.php?q=canada and shows a map.
Have a script that I can copy and run in mysql shell just fine,
I have done XML parsing before but never on a massive scale. If I'm
I Have done a site but want to redirect it to another subdomain in
I have done a hide and show of a div element, on clicking a

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.