export mysql tables in CVS into a ZIP file - php

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!

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);

Related

Appearance of user id when downloading to the file

There's this code which I have
function pushfile(&$file) {
header_remove();
if (isset($file["type"])) header('Content-Type: '.$file["type"]);
if (isset($file["name"])) header('Content-Disposition: attachment; filename="'.$file["name"].'"');
print $file["name"];
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: ");
echo $file["content"];
exit;
$h=fopen($filename,"r");
$content = fread($h, filesize($filename));
fclose($h);
$file["type"] = "application/"; //
$file["name"] = $real_filename;
$file["content"] = $content."*".$context['user']['id'];
pushfile($file);
This ID number was well placed into the downloaded file, but it disappeared after converting it (e.g. from pdf to epub). Is there a good solution for that?

ZIP file won't download and acts weird with php

I'm working on a system in laravel to make a zip full of photos and download it afterwards.
I decided i really don't want to use libraries for this(it is necessary), so I must use plain php. my controller code:
public function downloadPictures()
{
$pictures = Input::get('photos');
$file_paths = array();
foreach ($pictures as $picture_id) {
$path = $this->photos->getFilepath($picture_id, 'original');
array_push($file_paths, $path);
}
$zip = new ZipArchive;
$zip->open('slike.zip', ZipArchive::CREATE);
foreach ($file_paths as $file) {
$content = file_get_contents($file);
$zip->addFromString(basename($file), $content);
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zip->filename).'"');
echo var_dump($zip);
echo basename($zip->filename);
$zip->close();
echo var_dump($zip);
echo basename($zip->filename);
}
Now with the first part of the code i can say with 100% confidence that i get the right picture paths(that is above the //if (file_exists($file_paths ... ) comment, and i made sure by printing them out and using file_exists)
But then things start getting wonky.
Firstly: when i open the zip the numFiles i tested says there are allready 4 files in it, i have no idea why.
Secondly: when i print the response from this controller function in js frontend(using echo in controller var_dump($zip)) i get the zip file properties.. name+ numFile+4 (+4 for some reason), but when i $zip->close() i can't access the properites echoed zip properties are empty.
Third: the redirect headers, which i used in both ways: before i closed the $zip and after(currently they are before the closing) are not doing anything.. they should produce a donwload form in a browser, are they not?
If somebody could help me i would be so greatful. I need to do this due sunday and i have been fiddling around this for about 8hrs now.(this is my first time doing this). I have done quite a lot of googling and it works for others. I am on ununtu on nginx, php v5.6 and i installed the php zip extension and i am testing it localy on ubuntu mozilla browser.
UPDATE: It doesn't work in chrome either, so it's not firefoxes problem.
Try this:
public function downloadPictures()
{
$pictures = Input::get('photos');
$file_paths = array();
foreach ($pictures as $picture_id) {
$path = $this->photos->getFilepath($picture_id, 'original');
array_push($file_paths, $path);
}
$zip = new ZipArchive;
$zipname = 'silke.zip';
$zip->open($zipname, ZipArchive::CREATE);
foreach ($file_paths as $file) {
$content = file_get_contents($file);
$zip->addFromString(basename($file), $content);
}
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipname.'"');
header('Content-Length: ' . filesize($zipname));
$zip->close();
readfile($zipname);
}
Most important is the Content-Lengthand the readfileline.
Don't echo anything else, it will corrupt the download.
Update: A more selfcontained test:
$testData = array(
'test1.txt' => 'Test1',
'test2.txt' => 'Test2',
'test3.txt' => 'Test3',
'test4.txt' => 'Test4',
);
$zip = new ZipArchive;
$zipname = 'slike.zip';
$zip->open($zipname, ZipArchive::CREATE);
foreach ($testData as $filename => $content) {
$zip->addFromString($filename, $content);
}
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.$zipname.'"');
header('Content-Length: ' . filesize($zipname));
$zip->close();
readfile($zipname);
Also this has header('Content-Type: application/force-download');

Php or Apache? Content of zip file is diplayed instead of downloading zip file

I tried to write a php-script to create and download a zip file. When I tested the script on my localhost, the download works, but when it's uploaded to the server, things go wrong: instead of downloading the zip file, the content of the file is displayed in the browser.
Can somebody point me in the right direction?
The code
$zip = new ZipArchive();
$zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
$zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
$zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
$zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
$zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
$zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
$zip->close();
$filename = "zipfile.zip";
$filepath = "$maand/";
// headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
you missing ob_start()
example:
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
ob_start();
$str = '';
if(file_exists($file) === true){
$str = file_get_contents($file);
}
ob_end_clean();
echo $str;

Read and write using fgetcsv and fputcsv

I have a 3rd party source from where I am getting "csv" file. I wrote it inside a quote because it says it's a csv file but basically it's not.
So I am taking that main source file then reading and putting the data in a "PROPER" csv file.
The read and write is fine but the problem is when it saves the properly quoted data is writing on the script file itself.For example if the my php file name is "fixcsv.php" then I am getting the downloadable file as "fixcsv.php".
My code
$headings = array('HID');
$handle = fopen("MonC1.csv", "r");
$data = fgetcsv($handle, 0, ";",'"');
$fh = fopen('php://output', 'w');
ob_start();
fputcsv($fh, $headings);
// Loop over the * to export
if (! empty($data)) {
foreach ($data as $item) {
// echo $item;
fputcsv($fh, array($item));
}
}
$string = ob_get_clean();
$filename = 'csv_' . date('Ymd') .'_' . date('His');
// Output CSV-specific headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment filename=\"$filename.csv\";" );
header("Content-Transfer-Encoding: binary");
exit($string);
Any help is highly appreciated. Thanks in advance.
Your Content-Disposition has a semi-colon in the wrong place (per the spec). Should be:
header("Content-Disposition: attachment; filename=\"$filename.csv\" );

Create and download CSV file in one script [duplicate]

This question already has answers here:
Create a CSV File for a user in PHP
(20 answers)
Closed 8 years ago.
I want to create and download a CSV file, in one script.
Up to now I have been avoiding my lack of knowledge by pre-creating the file with a cron job and then
downloading the file via a link.
I have this:
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die ('Error connecting to mysql');
$get_members_csv_query = "SELECT * FROM members";
$get_members_csv_result=$conn->query($get_members_csv_query, MYSQLI_STORE_RESULT);
$all_members_array = array();
while ($get_members_csv_array = $get_members_csv_result->fetch_assoc())
{
array_push($all_members_array, $get_members_csv_array);
}
$file = fopen('members.csv', 'w');
fputcsv($file, array('email', 'First Name', 'Surname', 'Gender','DOB','Registered On','Country','paid','Children','Wants Kids','Relationship','Body Type','Height','Smoke','Drink','Phone','Region','Religion','Community'));
foreach ($all_members_array as $row) {
fputcsv($file, $row);
}
exit();
This creates the file successfully, but what do I need to add to the script to make it automatically download the CSV file once created?
I have tried putting this at the start if the script:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=members.csv");
header("Pragma: no-cache");
header("Expires: 0");
But it downloads the file before the script finishes (or even starts) creating the file.
I have tries putting the headers at the end of the script and using ob_start() at the top - same result.
Thanks
You're never sending the contents of the file to the browser. It should be:
$file = fopen('members.csv', 'w');
fputcsv($file, array('email', 'First Name', 'Surname', 'Gender','DOB','Registered On','Country','paid','Children','Wants Kids','Relationship','Body Type','Height','Smoke','Drink','Phone','Region','Religion','Community'));
foreach ($all_members_array as $row) {
fputcsv($file, $row);
}
fclose($file);
// Download file
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=members.csv");
header("Pragma: no-cache");
header("Expires: 0");
readfile('members.csv');
exit();
Try this:
From the exit() line, modify to:
exit(dnl("members.csv"));//call dnl function
then on the same page write the dnl function:
function dnl($fn){
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$fn");
header("Content-Transfer-Encoding: binary ");
}
Remember that the content-type "application/octet-stream" can be changed to match only csv as you have done before.

Categories