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

时间:2017-10-31 06:18:57

标签: 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);

}
}
?>

1 个答案:

答案 0 :(得分:0)

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

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