0
我从以前的帖子中找到了下面的脚本来使用PHP创建一个ZIP文件。如何使用PHP和目标目录创建压缩文件?
这个脚本可以让你创建一个zip文件,但它不允许你在你想要的任何目录下创建它。这是原始代码。
<?php
// Config Vars
$sourcefolder = "uploads/output" ; // Default: "./"
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip"
$timeout = 5000 ; // Default: 5000
// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);
// set script timeout value
ini_set('max_execution_time', $timeout);
// instantate object
$zip = new ZipArchive();
// create and open the archive
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close the archive
$zip->close();
echo "<br>Archive ". $zipfilename . " created successfully - ";
// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">Download <?php echo $zipfilename?></a>
我试图修改代码,以便能够决定脚本的desitnation因为我发现没有办法花了几个小时在互联网上之后。一些解决方案建议在哪里更改$ zipfilename =“myarchive.zip”;到$ zipfilename =“uploads/output/myarchive.zip”;但要下载的URL链接我要表现为:成功创建存档myarchive.zip - 下载成功创建myarchive.zip没有存档上传/输出/ myarchive.zip - 下载myarchive.zip
因此我试过这段代码,但它对我没有用。我不知道我做错了什么。
<?php
// Config Vars
$sourcefolder = "uploads/output" ; // Default: "./"
$destfolder = "uploads/output/"; // Default: "myarchive.zip"
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip"
$timeout = 5000 ; // Default: 5000
// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);
// set script timeout value
ini_set('max_execution_time', $timeout);
// instantate object
$zip = new ZipArchive();
// create and open the archive
if ($zip->open(''.$destfolder.''.$zipfilename.'', ZipArchive::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close the archive
$zip->close();
echo "<br>Archive ". $zipfilename . " created successfully - ";
// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">Download <?php echo $zipfilename?></a>