提问人:Sanjay Ojha 提问时间:7/21/2023 最后编辑:Professor AbronsiusSanjay Ojha 更新时间:7/21/2023 访问量:40
如何使用PHP ZipArchive类为创建的zip存档设置输出目录?
How to set output directory for the created zip archive using PHP ZipArchive class?
问:
我正在使用PHP类来存档来自不同位置的文件。代码如下ZipArchive
PHP的
$files = array('path/1/file1.zip', 'path/2/file2.zip');
$zipname = "path/to/output/archive-" . time() . ".zip"; // Zip name
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$data = file_get_contents($file);
$name = basename($file, '.zip') . '.zip';
if (file_exists($path)) {
$zip->addFromString($name, $data);
} else {
echo "file does not exist";
exit;
}
}
$zip->close();
问题是它在根目录中创建了一个带有名称文件的 zip 文件。我希望它会在文件夹内创建。path_to_output_archive-16783322.zip
archive-16783322.zip
path/to/output/
答: 暂无答案
评论
$zipname = __DIR__ . "/path/to/output/archive-" . time() . ".zip"
;$path
__DIR__
path/to/output
是一个相对路径,除非你知道PHP进程的工作目录,否则你无法知道它从哪里开始。但这通常是主文件的位置。