duankan8739 2012-03-30 05:35
浏览 46

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

I have a function that takes uploaded files (WORDPRESS) and adds them to a (newly created) zip file. every new file is added to the zip (if is not yet created - the first file will create one ) and also to a comment with the list of the files.

 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
    }

My problem : if I try to upload any file other than an image - the function will DIE . I have added a condition for checking imagetype - but I would like to know why it is crashing and how to make it work without said condition ... Does the zip function have any problems with PDF , doc or anyother ? is that a wordpress problem ?

  • 写回答

1条回答 默认 最新

  • dtkl55257 2012-03-30 05:42
    关注

    The problem section seems to be where you're asking PDFs, etc. their image size. Why don't you try:

    $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.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本