下载zip文件的PHP脚本
我正在我的服务器上设置一个脚本,它使用用户发送的一些GET变量来决定在zip中包含哪些文件,然后返回给用户。ZIP文件是由脚本动态创建的,它的名称始终为myZip.ZIP 如果我可能有100个用户同时调用该脚本,并且每个用户的zip中可能包含不同的文件,那么这会起作用吗 谢谢 脚本示例:下载zip文件的PHP脚本,php,zip,ziparchive,Php,Zip,Ziparchive,我正在我的服务器上设置一个脚本,它使用用户发送的一些GET变量来决定在zip中包含哪些文件,然后返回给用户。ZIP文件是由脚本动态创建的,它的名称始终为myZip.ZIP 如果我可能有100个用户同时调用该脚本,并且每个用户的zip中可能包含不同的文件,那么这会起作用吗 谢谢 脚本示例: <?php $fileA = 'fileA.xml'; $fileB = 'fileB.xml'; $fileC = 'fileC.xml'; $filesTo
<?php
$fileA = 'fileA.xml';
$fileB = 'fileB.xml';
$fileC = 'fileC.xml';
$filesToSend = array();
if(conditionA) {
array_push($filesToSend, $fileA);
}
if(conditionB) {
array_push($filesToSend, $fileB);
}
if(conditionC) {
array_push($filesToSend, $fileC);
}
if(count($filesToSend) > 0) {
$zipname = "myZip.zip";
$zip = new ZipArchive();
$res = $zip->open($zipname, ZipArchive::CREATE);
if ($res === TRUE) {
foreach ($filesToSend as $file) {
$zip->addFile($file);
}
}
else {
echo 'failed, code:' . $res;
}
$zip->close();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/zip");
header('Content-length: '.filesize($zipname));
header('Content-disposition: attachment; filename=' . basename($zipname));
ob_clean();
flush();
readfile($zipname);
@unlink($file);
}
else {
echo "nothing new";
echo $modifiedDate;
}
?>
这些链接可以帮助您:
您应该在服务器上为每个用户创建一个唯一的文件名,然后为每个oneNope提供“myZip.zip”。您正在创建一个on-disk.zip文件,因此您的所有用户都将互相踩踏。使用
uniqid()
为每个请求生成唯一的文件名,并将该id用于磁盘上的文件名。您仍然可以在下载标题中提供“myZip.zip”,例如内容配置:附件;filename=myZip.zip
链接仅回答问题,不可接受。第二个链接甚至没有答案,也没有被社区接受的答案。