PHP动态zip文件创建因非图像文件类型而崩溃。(wp)

PHP动态zip文件创建因非图像文件类型而崩溃。(wp),php,wordpress,zip,Php,Wordpress,Zip,我有一个函数,可以将上传的文件(WORDPRESS)添加到(新创建的)zip文件中。 每个新文件都会添加到zip(如果尚未创建,则第一个文件将创建一个),并添加到带有文件列表的注释中 function Ob99_generate_zip_file($meta) { // we always need post_id , right ? if( isset($_GET['post_id']) ) { $post_id = $_GET['post_id']

我有一个函数,可以将上传的文件(WORDPRESS)添加到(新创建的)zip文件中。 每个新文件都会添加到zip(如果尚未创建,则第一个文件将创建一个),并添加到带有文件列表的注释中

 function Ob99_generate_zip_file($meta) {
     // we always need post_id , right ?
    if( isset($_GET['post_id']) ) {
            $post_id = $_GET['post_id'];
        } elseif( isset($_POST['post_id']) ) {
            $post_id = $_POST['post_id'];
        }
        //setting some more variables.
        $file =  wp_upload_dir();// take array
        $file2 = wp_upload_dir();//debug
        $zipname = $file['path'].'file.zip'; // set zip file name
        $file = trailingslashit($file['basedir']).$meta['file'];// construct real path

            // Without this next condition the function dies. WHY ??
        list($orig_w, $orig_h, $orig_type) = @getimagesize($file); // not help to comment
        if (!$orig_type == IMAGETYPE_GIF || !$orig_type == IMAGETYPE_PNG|| !$orig_type == IMAGETYPE_JPEG) { 
         //why other filetypes not working ??
         return ;
        }

    $zip = new ZipArchive; // initiatte class
    $zip->open($zipname , ZipArchive::CREATE); // open buffer
    $new_filename = substr($file,strrpos($file,'/') + 1); //we do not need nested folders
    $zip->addFile($file,$sitename.'/'.$new_filename); // we add the file to the zip
    if (file_exists($zipname)){
    $comment = $zip->getArchiveComment();  // if the file already exist read the comment
    } 
    else { // if not - let´s give it a cool retro header
    $comment_head = '*********************************'. PHP_EOL ;
    $comment_head .= '****** <<< FILE CONTENT >>> *****'. PHP_EOL ;
    $comment_head .= '*********************************'. PHP_EOL ;

    }
    $comment = $comment_head . $comment ;// add header before comment
    $comment = $comment . PHP_EOL . PHP_EOL . $meta['file'] ; // add new file name
    $zip->setArchiveComment($comment); // and comment
    $zip->addFromString('filelist.txt', $comment); // create txt file with the same list
    $zip->close()or die('can not create zip file'.$file.print_r($meta).'---- DEBUG SEPERATOR ---- '.print_r($file2));  // FINISHED or DIE with debug
    }
函数Ob99\u生成zip\u文件($meta){
//我们总是需要邮寄身份证,对吗?
如果(isset($\u GET['post\u id'])){
$post\u id=$\u GET['post\u id'];
}elseif(isset($\u POST['POST\u id'])){
$post\u id=$\u post['post\u id'];
}
//设置更多变量。
$file=wp_upload_dir();//获取数组
$file2=wp_upload_dir();//调试
$zipname=$file['path'].'file.zip';//设置zip文件名
$file=trailingslashit($file['basedir'])。$meta['file'];//构造真实路径
//如果没有下一个条件,函数将终止。为什么??
list($orig_w,$orig_h,$orig_type)=@getimagesize($file);//不帮助评论
如果(!$orig_type==IMAGETYPE|GIF |!$orig_type==IMAGETYPE|PNG |!$orig_type==IMAGETYPE|JPEG){
//为什么其他文件类型不工作??
返回;
}
$zip=new ZipArchive;//初始化类
$zip->open($zipname,ZipArchive::CREATE);//打开缓冲区
$new_filename=substr($file,strrpos($file,“/”)+1);//我们不需要嵌套文件夹
$zip->addFile($file,$sitename.'/'.$new_filename);//我们将文件添加到zip
如果(文件_存在($zipname)){
$comment=$zip->getArchiveComment();//如果文件已经存在,请读取注释
} 
否则{//如果没有-让我们给它一个很酷的复古标题
$comment_head='*****************************************'.PHP_EOL;
$comment_head.='******>******'.PHP_EOL;
$comment\u head.='******************************************.'.PHP\u EOL;
}
$comment=$comment\u head.$comment;//在注释之前添加标题
$comment=$comment.PHP_EOL.PHP_EOL.$meta['file'];//添加新文件名
$zip->setArchiveComment($comment);//和comment
$zip->addFromString('filelist.txt',$comment);//使用相同的列表创建txt文件
$zip->close()或die('无法创建zip文件'.$file.print_r($meta)。'----调试分隔符--'.print_r($file2));//完成调试或使用调试结束
}
我的问题是:如果我试图上传图像以外的任何文件,该功能将失效。 我已经为检查imagetype添加了一个条件-但我想知道为什么它会崩溃,以及如何在没有该条件的情况下使它工作。。。
zip函数在PDF、doc或其他方面有问题吗?这是wordpress的问题吗

问题部分似乎是您询问PDF等图像大小的地方。你为什么不试试:

$image_size = getimagesize($file);

if($image_size===false)
{
   // This is not an image
   // Do what you want to PDFs, etc.
}
else
{
   // This is an image
   // Find out image type, dimensions, etc.
}

谢谢-我已经更正了问题帖子-但无论如何-评论只针对SE上的这篇帖子。它们不在原始代码中。感谢您花时间查看它-但我想您误解了问题(可能我对代码的注释不够)。我获取imagesize的部分只是为了过滤非图像文件而添加的。就像我在代码注释中写的那样。我不需要图像大小或mimetype-但如果我不这样做,如果条件-它将死亡时加载PDF的所有相同。我这样做是为了在有人加载PDF时不破坏整个系统(只是忽略它)。但这不是我真正想要的。为什么你甚至需要过滤非图像文件?如果你想压缩所有上传的文件,为什么要添加这个条件?因为如果我没有这个条件,函数就会失效。这就是我问题的关键所在-如果删除此条件并且非图像文件获得通过,它为什么会死亡…您能否清除
或die()
,并查看它是否产生错误消息?它应该告诉你哪里出了问题。