2016-12-18 10 views
0

現在、私は現在、PHPで作成されたツール(この技術ではかなり初心者です...)の中に、一連のファイルを含むzipファイルを生成する必要があります。ファイルのこのセットは指定できますPHP - ジッパーをジップに追加するときに破損したジップ

  • 基本ファイル(mutliple形式)
  • 全ディレクトリ(新しいZIPファイルとして結果のzipに追加されます - 最終ZIP内郵便)

問題は、zipファイルにシンプルファイルが含まれている場合は、適切にダウンロードされますが、ファイルに "フルディレクトリzipファイル"が含まれている場合、結果のZIPファイルが破損することです...

コードの下に私は現在使用しています少し面倒ですが初めてのときです

  • ます。ZipFiles::)... PHPでそれに

    function ZipFiles($fileArr,$id) { 
        $destination = "{$_SERVER['DOCUMENT_ROOT']}/wmdmngtools/tempFiles/WMDConfigFiles_".$id.".zip"; 
        $valid_files = array(); 
    
        //if files were passed in... 
        if(is_array($fileArr)) { 
         //cycle through each file 
         foreach($fileArr as $file) {    
    
          if(is_dir($file)) { 
           //If path is a folder we zip it and put it on $valid_files[] 
           $resultingZipPath = "{$_SERVER['DOCUMENT_ROOT']}/wmdmngtools/tempFiles/".basename($file)."_FOLDER.zip"; 
           ZipFolder($file,$resultingZipPath); 
           $valid_files[] = $resultingZipPath ;  
          } 
          else {    
           //If path is not a folder then we make sure the file exists 
           if(file_exists("{$_SERVER['DOCUMENT_ROOT']}/wmdmngtools/tempFiles/".$file)) { 
            $valid_files[] = $file;     
           } 
          } 
         } 
        } 
        //if we have good files... 
        if(count($valid_files)) { 
         //create the archive 
         $zip = new ZipArchive(); 
         if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) { 
          return false; 
         } 
         //add the files 
         foreach($valid_files as $file) { 
          $zip->addFile("{$_SERVER['DOCUMENT_ROOT']}/wmdmngtools/tempFiles/".$file,$file); 
         } 
    
         $zip->close(); 
    
         return $destination; 
        } 
        else 
        { 
         return ""; 
        } 
    } 
    
    function ZipFolder($source, $destination) { 
    
        // Initialize archive object 
        $folderZip = new ZipArchive(); 
        $folderZip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE); 
    
        // Create recursive directory iterator 
        /** @var SplFileInfo[] $files */ 
        $files = new RecursiveIteratorIterator(
         new RecursiveDirectoryIterator($source), 
         RecursiveIteratorIterator::LEAVES_ONLY 
        ); 
    
        foreach ($files as $name => $file) 
        { 
         // Skip directories (they would be added automatically) 
         if (!$file->isDir()) 
         { 
          // Get real and relative path for current file 
          $filePath = $file->getRealPath(); 
          $relativePath = substr($filePath, strlen($source) + 1); 
    
          // Add current file to archive 
          $folderZip->addFile($filePath, $relativePath); 
         } 
        } 
    
        // Zip archive will be created only after closing object 
        $folderZip->close(); 
    
    } 
    

    を働く我々は2つの機能を見ることができるリスト/配列を渡すことによって呼び出されるメインfucntionは、(ファイルのリストが含まれていると最終的なZIPに追加されるフォルダ)と、異なるファイル名を生成するために単純に使用されるIDパラメータ...(無視することができます)

  • ZipFolder:この機能は、そのフォルダを解凍してzipファイルを作成して最終ファイルに追加します。 (私はHow to zip a whole folder using PHPで見つかったものに基づいて)

私はすべてのファイルを閉じる、またはzip内の空のジッパーを避けるように上記の記事で言及したが、何も働いていないような多くのことを試してみました...

Zip inside zip (php)

多分私は何かを逃してしまったかもしれませんが、エースが足りなくなっているので、どんな助けやガイドラインもありがとうございます。

さらに詳しい情報が必要な場合は、私に知らせて投稿してください。

ありがとうございます!

+0

誰かが私が間違っているかもしれない何か手がかりを持っていますか? :) –

答えて

0

最後に問題が見つかりました。ファイルが適切に生成されたと思われますが、PHPからダウンロードするときに、サイズが具体的な数値よりも大きいときに問題がありました。私は推測している場合でも、それは私がのContent-Lengthエントリを削除し、今私はのにもかかわらず、正しいファイルを取得し、正しい練習ができない場合があり

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Length: ".filesize($zippedFile)); 
header("Content-Disposition: attachment; filename=".$zippedFile); 
header("Content-type: application/zip"); 
header("Content-Transfer-Encoding: binary"); 

: これは、ヘッダー定義上のメッセージ長の誤った定義によるものでしたその大きさ。

関連する問題