我有一个脚本:
- 浏览我的"./sample/"目录中的每个文件夹和子文件夹
- 打开每个.docx文件
- 用$_POST['PROPERTY']变量替换诸如"###PROPERTY##"之类的字符串
- 压缩文件夹内容
- 启动下载
现在,单独运行部分代码,它就完成了需要的操作。然而,当把所有这些放在一起时,它会在扫描子文件夹中的docx文件时死亡。
我的文件夹结构如下:
/样品/
- /IT/it1.docx
- /F&B/fb1.docx
- /FO/fo1.docx
- 示例1.docx
问题似乎发生在级别1文件夹的is_dir($dir)部分。你知道是什么原因造成的吗?
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// form variables
$holidex = strtoupper($_POST['holidex']);
$property = $_POST['property'];
$brand = $_POST['brand'];
$division = $_POST['division'];
$language = $_POST['language'];
$date_issued = $_POST['date_issued'];
$approved_by = $_POST['approved_by'];
// script variables
//$dir = './Sample_SOP_-_'.$brand.'_('.$language.')'; //dir to scan like ./sample/
$dir = './sample/'; //dir to scan like ./sample/
$archive = date("Y-m-d H-i-s"); //UNIQUE name of zip file to create and download
$zipfile = "./".$holidex." - ".$archive.".zip"; //path to zip file download
$temp = "./temp/"; //directory to temp folder
// string replacements
$find = "##PROPERTY##"; // find and replace property information
$replace = $_POST['property'];
$find2 = "##BRAND##"; // find and replace brand information
$replace2 = $_POST['brand'];
$find3 = "##DATE##"; // find and replace effective date
$replace3 = $_POST['date_issued'];
$find4 = "##APPROVED_BY##"; // find and replace approved by
$replace4 = $_POST['approved_by'];
//read dir
$files = scandir($dir, 1);
//create new archive name
$zip_download = new ZipArchive();
$zip_download->open("$zipfile", ZipArchive::CREATE);
foreach($files as $file) {
//docx
$ext1 = ".docx";
$checkextension = explode($ext1, $file);
if (count($checkextension) > 1) {
$zip = new ZipArchive;
$zip->open("$file");
$word = $zip->getFromName('word/document.xml');
$word2 = str_replace($find, $replace, $word);
$word2 = str_replace($find2, $replace2, $word);
$word2 = str_replace($find3, $replace3, $word);
$word2 = str_replace($find4, $replace4, $word);
$zip->addFromString("word/document.xml", $word2);
$zip->close();
} else {
die("Error - There are no files the directory..");
}
//folders level 1
if (is_dir($file)) {
$sub = $file . '/';
$subfiles = scandir($sub, 1);
if ($subfiles > 1) {
if ($sub == "../" || $sub == "./") {
}
else {
foreach($subfiles as $subfile) {
//docx
$ext1 = ".docx";
$checkextensionsub = explode($ext1, $subfile);
$subsubfile = $sub . $subfile;
if (count($checkextensionsub) > 1) {
$zipsub = new ZipArchive;
$zipsub->open("$subsubfile");
$wordsub = $zipsub->getFromName('word/document.xml');
$word2sub = str_replace($find, $replace, $wordsub);
$word2sub = str_replace($find2, $replace2, $wordsub);
$word2sub = str_replace($find3, $replace3, $wordsub);
$word2sub = str_replace($find4, $replace4, $wordsub);
$zipsub->addFromString("word/document.xml", $word2sub);
$zipsub->close();
}
//folders level 2
$sub2 = $sub . $subfile;
if (is_dir($sub2)) {
$subfiles2 = scandir($sub2, 1);
if ($subfiles2 > 1) {
if ($sub2 == $sub.".." || $sub2 == $sub.".") {
}
else {
foreach($subfiles2 as $subfile2) {
//docx
$ext1 = ".docx";
$checkextensionsub2 = explode($ext1, $subfile2);
$subsubfile2 = $sub2 . '/' . $subfile2;
if (count($checkextensionsub2) > 1) {
$zipsub2 = new ZipArchive;
$zipsub2->open("$subsubfile2");
$wordsub2 = $zipsub2->getFromName('word/document.xml');
$word2sub2 = str_replace($find, $replace, $wordsub2);
$word2sub2 = str_replace($find2, $replace2, $wordsub2);
$word2sub2 = str_replace($find3, $replace3, $wordsub2);
$word2sub2 = str_replace($find4, $replace4, $wordsub2);
$zipsub2->addFromString("word/document.xml", $word2sub2);
$zipsub2->close();
}
//more directories when needed
//****replicate code here****
//add files to archive
$zip_download->addFile($subsubfile2, $subsubfile2);
}
}
}
}
//add files to archive
$zip_download->addFile($subsubfile, $subsubfile);
}
}
}
} else {
die ("Error - No files in the directory");
}
}
//add files to archive
$zip_download->addFile($file, $file);
}
$zip_download->close();
//download zip
if (file_exists($zipfile) && is_readable($zipfile)) {
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($zipfile));
header('Content-Disposition: attachment; filename="'.basename($zipfile).'";');
header('Content-Transfer-Encoding: binary');
$file_download = @ fopen($zipfile, 'rb');
if ($file_download) {
fpassthru($file_download);
exit;
}
echo ("ZIP generated successfully, download is starting...");
} else {
echo ("Error creating the ZIP archive!");
}
}
?>
我认为这是因为Scandir()会产生类似的东西
`Array
(
...
[9] => .
[10] => ..`
在数组的末尾,当你检查这些是否是文件夹时,可能会发生错误并导致死亡。