繁体   English   中英

使用php从数据库中解压缩并下载文件夹和文件夹名称

[英]zip and download a folder and the folder name from database using php

我正在尝试以zip格式下载文件夹,该文件夹名称保存在我的数据库表中。 我的代码不起作用,相同的代码显示为输出。 我的表名是pancard。 我还想要将数据从数据库表转换为xls的代码。 请帮我解决。 提前致谢...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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