2012-03-30 56 views
3

我有一个函数将上传的文件(WORDPRESS)添加到(新创建的)zip文件中。 每个新文件都被添加到zip(如果尚未创建 - 第一个文件将创建一个),并且还会将文件列表添加到注释中。PHP动态zip文件创建与非图像文件类型崩溃。 (wp)

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的问题?

回答

0

问题部分似乎是您要求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. 
} 
+0

谢谢 - 我已更正了问题发布 - 但无论如何 - 评论是只为这个职位在SE。它们不在原始代码中。 – 2012-03-30 05:44:15

+0

感谢您花时间看看它 - 但我想你误解了问题(也许我没有足够的评论代码)。我只能添加图像的部分,以便过滤非图像文件。就像我在代码评论中写的一样。我不需要图像大小,也不需要mimetype - 但是如果我不这样做,如果条件 - 它会在加载PDF时完全一样。我这样做是为了不打破整个系统,如果有人加载PDF(并忽略它)。但这不是我真正想要的。 – 2012-03-30 08:54:10

+0

为什么你甚至需要过滤非图像文件?如果你想压缩_all_上传的文件,为什么要添加这个条件? – hohner 2012-03-30 09:27:53