我有一个php脚本可以用2个函数制作zip:
- dirtoArray:要在数组中获取所有文件/empty_folder
- create_zip:致电diretoArray()并制作ziparchive
我得到了一个怪异的警告,这实际上是一个真正的错误,因为我的邮政编码不是构建的。
警告结果
警告:ziparchive :: close():读取错误:路径/to/file.php in Line x
中的糟糕文件描述符
有人可以向我解释什么意思:"坏文件描述符"?
这是代码:
dirtoArray
/* to copy all file/folder names from a directory into an array*/
function dirToArray($dir_path) {
$result = array();
$path = realpath($dir_path);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object) {
if( $object->getFilename() !== "." && $object->getFilename() !== "..") {
$result[] = $object;
}
}
return $result;
}
create_zip
/* creates a compressed zip file */
function create_zip($productPath = '', $dirName = '', $overwrite = false) {
$fullProductPath = $productPath.$dirName;
$a_filesFolders = dirToArray( $fullProductPath );
var_dump($a_filesFolders);
//if the zip file already exists and overwrite is false, return false
$zip = new ZipArchive();
$zipProductPath = $fullProductPath.'.zip';
if($zip->open( $zipProductPath ) && !$overwrite){
$GLOBALS["errors"][] = "The directory {$zipProductPath} already exists and cannot be removed.";
}
//if files were passed in...
if(is_array($a_filesFolders) && count($a_filesFolders)){
$opened = $zip->open( $zipProductPath, ZipArchive::CREATE | ZipArchive::OVERWRITE );
if( $opened !== true ){
$GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it.";
}
//cycle through each file
foreach($a_filesFolders as $object) {
//make sure the file exists
$fileName = $object -> getFilename();
$pathName = $object -> getPathname();
if(file_exists($pathName)) {
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
echo $pathName.'<br/>';
echo $fileDestination.'<br/>';
$zip->addFile($pathName,$fileDestination);
}
else if(is_dir( $pathName )){
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addEmptyDir($fileDestination);
}else{
$GLOBALS["errors"][] = "the file ".$fileName." does not exist !";
}
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($zipProductPath);
}else{
return false;
}
}
我发现了问题...我对File_exists()函数感到困惑,该函数也检测到目录是否存在...因此脚本添加了文件夹并制作了错误。<<<<<<<<<<<<<<<<
create_zip(修补)
/* creates a compressed zip file */
function create_zip($productPath = '', $dirName = '', $overwrite = false) {
$fullProductPath = $productPath.$dirName;
$a_filesFolders = dirToArray( $fullProductPath );
var_dump($a_filesFolders);
//if the zip file already exists and overwrite is false, return false
$zip = new ZipArchive();
$zipProductPath = $fullProductPath.'.zip';
if($zip->open( $zipProductPath ) && !$overwrite){
$GLOBALS["errors"][] = "The directory {$zipProductPath} already exists and cannot be removed.";
return false;
}
//if files were passed in...
if(is_array($a_filesFolders) && count($a_filesFolders)){
$opened = $zip->open( $zipProductPath, ZipArchive::CREATE | ZipArchive::OVERWRITE );
if( $opened !== true ){
$GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it.";
return false;
}else{
//cycle through each file
foreach($a_filesFolders as $object) {
//make sure the file exists
$fileName = $object -> getFilename();
$pathName = $object -> getPathname();
if(is_dir( $pathName )){ /*<-- I put on first position*/
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addEmptyDir($fileDestination);
}else if(file_exists($pathName)) {
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addFile($pathName,$fileDestination);
}
else{
$GLOBALS["errors"][] = "the file ".$fileName." does not exist !";
}
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($zipProductPath);
}
}else{
return false;
}
}