繁体   English   中英

如何下载压缩文件的子级之一

[英]How to download one of the children of a zipped file

我在PHP中创建一个压缩文件,如下所示:

$nameFile = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmp_name'];

$fp      = fopen($tmpName, 'r');
$containFile = fread($fp, filesize($tmpName));
fclose($fp);

$zip = new ZipArchive();

$fileconpress = "uploads/".$nameFile.".zip";

$conpress = $zip->open($fileconpress, ZIPARCHIVE::CREATE);
if ($conpress)
{
$zip->addFromString($nameFile, $containFile);
$zip->close();

一切正常。 在另一页中,我列出了文件夹中所有的压缩文件以及这些压缩文件中的所有子项(通过数据库获取列表)。

我希望能够下载压缩文件中的1个孩子。 如何打开服务器上的压缩文件并让用户下载其中之一?

我检查php.net并找到zip_read和zip_open,但是它们并没有提供太多示例,因此我很难理解它的真正工作原理。

php文档中有很多示例: http : //php.net/manual/en/function.zip-open.php

只需打开文件,将其解压缩到目录,然后将其作为所需服务器即可。 此设置使我感到疑问,您是否应该准备好将服务器上的解压缩文件准备就绪,但这是您的选择。

$zip = new ZipArchive;
$res = $zip->open("uploads/".$parent.".zip");
if ($res === TRUE) {
    $zip->extractTo("uploads/".$parent);
    $zip->close();
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$child.'');
    header('Pragma: no-cache');
    readfile("uploads/".$parent."/".$child);
} else {
    echo 'failed, code:' . $res;
}

暂无
暂无

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

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