Php 将所有文件压缩为 zip 的代码无法正常工作
Php code to compress all files as zip not working fine
我有这个脚本可以从数据库中以 zip 格式下载所有文件,但它只获取数据库中保存的第一个文件 - 其他文件没有显示。
下面是我在数据库中的文件列表,我想将其包含在 zip 中:
lginin
logersutil.php
lgininh.js
Readme.md
还有我的代码:
<?php
if(isset($_POST['download'])){
try{
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
$post_stmt->execute();
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){
$files = array(''.$rowss["jailchillink"].'','codejail.cj');
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
} }catch (PDOException $e){ echo 'Connection failed: ' . $e->getMessage();}
}
?>
在您当前的代码中,您基本上是在制作 1 个新的 zip 文件并为每一行返回它。由于客户端只能读取 1 个响应,他们显然不会看到超过 1 行。
所以您需要做的是,确保在 发送之前将所有文件添加到 zip 。像这样:
<?php
if(isset($_POST['download'])){
try{
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
$post_stmt->execute();
// here we create a temporary array to hold all the filenames
// and fill it with the file you always want to have inserted
$files = array('codejail.cj');
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){
// now fill the array with your files
$files[] = $rowss['jailchillink'];
}
// and now we finally start processing them all, after having finished reading from the DB
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
} catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
}
?>
我有这个脚本可以从数据库中以 zip 格式下载所有文件,但它只获取数据库中保存的第一个文件 - 其他文件没有显示。
下面是我在数据库中的文件列表,我想将其包含在 zip 中:
lginin
logersutil.php
lgininh.js
Readme.md
还有我的代码:
<?php
if(isset($_POST['download'])){
try{
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
$post_stmt->execute();
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){
$files = array(''.$rowss["jailchillink"].'','codejail.cj');
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
} }catch (PDOException $e){ echo 'Connection failed: ' . $e->getMessage();}
}
?>
在您当前的代码中,您基本上是在制作 1 个新的 zip 文件并为每一行返回它。由于客户端只能读取 1 个响应,他们显然不会看到超过 1 行。
所以您需要做的是,确保在 发送之前将所有文件添加到 zip 。像这样:
<?php
if(isset($_POST['download'])){
try{
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
$post_stmt->execute();
// here we create a temporary array to hold all the filenames
// and fill it with the file you always want to have inserted
$files = array('codejail.cj');
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){
// now fill the array with your files
$files[] = $rowss['jailchillink'];
}
// and now we finally start processing them all, after having finished reading from the DB
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
} catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
} ?>