PHP动态zip文件创建与非图像文件类型崩溃.(wp)


PHP dynamic zip file creation crashes with non-image filetypes. (wp)

我有一个函数,它获取上传的文件(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
    }

我的问题是:如果我试图上传图像以外的任何文件,该功能就会失效。我添加了一个检查图像类型的条件,但我想知道它为什么崩溃,以及如何在没有上述条件的情况下使其工作。。。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.
}