简体   繁体   中英

zip and download a folder and the folder name from database using php

I am trying to download a folder as zip, the folder name is saved in my database table. My code is not working, The same code displaying as output. My table name is pancard. And I also want the code for converting the datas to xls from the databse table. Please Help me with a solution. Thanks in Advance...

    **HTML**
<form action="zip.php" method="POST">
<button type="submit" class="btn" name="zip">Download</button>
</form>

    **PHP**
<?php// Get real path for our folder

include_once 'includes/db_connect.php';

if (isset($_POST['zip'])){

$sql = "SELECT folder FROM `pancard`";
$result = mysqli_query ($dbcon, $sql); 
while ($row = mysqli_fetch_array ($result)){
$rootPath = realpath('../pancard/'.$row['folder'].'/');


$file = 'file.zip';
$zip = new ZipArchive();
$zip->open('$file', ZipArchive::CREATE | ZipArchive::OVERWRITE);



$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{

if (!$file->isDir())
{

    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);


    $zip->addFile($filePath, $relativePath);
}
}


$zip->close();

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

}
}
?>

$zip->open('$file', ZipArchive::CREATE | ZipArchive::OVERWRITE);

$zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM