2012-03-30 7 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 
    } 

私の問題:イメージ以外のファイルをアップロードしようとすると、その機能はDIEになります。 私はimagetypeをチェックするための条件を追加しました - しかし、なぜそれがクラッシュしていることを知りたいのですが、上記の条件なしで動作させる方法を教えてください... zip関数はPDF、doc、anyotherに問題はありますか?それはワードプレスの問題ですか?

答えて

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でこのポストのためにのみ行われました。彼らは元のコードではありません。 –

+0

時間を割いてくれてありがとう - しかし、あなたは問題を誤解しています(多分私はコードをコメントしなかったかもしれません)。私が画像を取得する部分は、非画像ファイルをフィルタするために追加されただけです。私はコードコメントに書いたように。私はイメージのサイズもmimetypeも必要ありません - しかし、もし私がそれをしなければ条件 - それはすべて同じPDFを読み込むときに死ぬでしょう。私は誰かがPDFを読み込んで(それを無視するだけで)システム全体を壊さないために、この条件を実行します。それは私が本当に欲しいものではありません。 –

+0

なぜ非画像ファイルをフィルタリングする必要がありますか? _all_でアップロードしたファイルを圧縮するには、なぜこの条件を追加していますか? – hohner

関連する問題