我正在努力解决一个简单的 PHP 功能:创建包含一些文件的 ZIP 存档。
问题是,它不只创建一个名为 filename.zip 的文件,而是创建两个名为 filename.zip.a07600 和 filename.zip.b07600 的文件。请。请参阅以下屏幕截图:
这两个文件的大小非常完美,我什至可以将它们重命名为 filename.zip 并毫无问题地将其解压。
谁能告诉我出了什么问题吗???
function zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path = array(), $files_array = array()) {
// Archive File Name
$archive_file = $archiveDir."/".$archive_file_name;
// Time-to-live
$archiveTTL = 86400; // 1 day
// Delete old zip file
@unlink($archive_file);
// Create the object
$zip = new ZipArchive();
// Create the file and throw the error if unsuccessful
if ($zip->open($archive_file, ZIPARCHIVE::CREATE) !== TRUE) {
$response->res = "Cannot open '$archive_file'";
return $response;
}
// Add each file of $file_name array to archive
$i = 0;
foreach($files_array as $value){
$expl = explode("/", $value);
$file = $expl[(count($expl)-1)];
$path_file = $file_path[$i] . "/" . $file;
$size = round((filesize ($path_file) / 1024), 0);
if(file_exists($path_file)){
$zip->addFile($path_file, $file);
}
$i++;
}
$zip->close();
// Then send the headers to redirect to the ZIP file
header("HTTP/1.1 303 See Other"); // 303 is technically correct for this type of redirect
header("Location: $archive_file");
exit;
}
调用该函数的代码是一个带有 switch-case 的文件...它由 ajax 调用调用自身:
case "zdl":
$files_array = array();
$file_path = array();
foreach ($dbh->query("select GUID, DIRECTORY, BASENAME, ELEMENTID from SMDMS where ELEMENTID = ".$osguid." and PROJECTID = ".$osproject.";") as $subrow) {
$archive_file_name = $subrow['ELEMENTID'].".zip";
$archiveDir = "../".$subrow['DIRECTORY'];
$files_array[] = $archiveDir.DIR_SEPARATOR.$subrow['BASENAME'];
$file_path[] = $archiveDir;
}
zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path, $files_array);
break;
还有一个代码...我尝试将最新的 123456.zip.a01234 文件重命名为 123456.zip,然后使用此函数取消旧的 123456.zip.a01234 (以及所有先前添加的 .a01234 文件)的链接:
function zip_file_exists($pathfile){
$arr = array();
$dir = dirname($pathfile);
$renamed = 0;
foreach(glob($pathfile.'.*') as $file) {
$path_parts = pathinfo($file);
$dirname = $path_parts['dirname'];
$basename = $path_parts['basename'];
$extension = $path_parts['extension'];
$filename = $path_parts['filename'];
if($renamed == 0){
$old_name = $file;
$new_name = str_replace(".".$extension, "", $file);
@copy($old_name, $new_name);
@unlink($old_name);
$renamed = 1;
//file_put_contents($dir."/test.txt", "old_name: ".$old_name." - new_name: ".$new_name." - dirname: ".$dirname." - basename: ".$basename." - extension: ".$extension." - filename: ".$filename." - test: ".$test);
}else{
@unlink($file);
}
}
}
简而言之:复制有效,重命名不起作用,“取消链接”根本不起作用...我现在没有主意了...:(
再试一次:我将 $zip->getStatusString() 的输出放入变量中并将其写入日志文件...它生成的日志条目是:重命名临时文件失败:没有这样的文件或目录。 但正如您在上图中看到的,文件 43051221.zip.a07200 位于 zip-lib 临时打开它的目录中。
预先感谢您的帮助!
最佳答案
所以,经过几天的努力...就这么简单:
实际上,我只在 *nix 服务器上工作,因此在我的脚本中,我使用 0777 Perms 动态创建了文件夹。我不知道IIS根本不接受这种权限格式!
因此,我远程到服务器,右键单击“文档”文件夹(所有动态添加的文件和文件夹的层次结构最上面的文件夹)并为我找到的所有用户提供完全控制权.
现在完美了!!!现在唯一有趣的是:这是否有任何危险???
感谢您的善意答复...
关于php - 在通过 php 创建 zip 文件时,我得到两个文件而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33779211/