1

CakePHP 内でファイルとフォルダーを操作しています。今ではすべてがうまく機能し、希望どおりに機能しています。ただし、ファイルを圧縮すると、次のエラー メッセージが表示されます。

Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 240047685 bytes)  

小さなファイルを圧縮するようになりました。問題ありません。サイズが約 10MB のファイルでも問題なく実行できましたが、サイズが大きい圧縮には問題があるようです。

今、私は .htaccess ファイルに以下を追加し、php.ini ファイルを作成しました。

php_value upload_max_filesize 640000000M
php_value post_max_size 640000000M
php_value max_execution_time 30000000
php_value max_input_time 30000000

PHPが4GBのファイル制限であるという事実を指摘する投稿を見つけるまで。その場合でも、なぜ私のzipファイルはこのファイルを実行しないのですか(約245MBしかありません)。

   public function ZippingMyData() {
     $UserStartPath = '/data-files/tmp/';
     $MyFileData = $this->data['ZipData']; //this is the files selected from a form!

      foreach($MyFileData as $DataKey => $DataValue) {
        $files = array($UserStartPath.$DataValue);
        $zipname = 'file.zip';
        $zip = new ZipArchive();
        $zip_name = time().".zip"; // Zip name
        $zip->open($zip_name,  ZipArchive::CREATE);

        foreach ($files as $file) {
         $path = $file;
                if(file_exists($path)) {
            $zip->addFromString(basename($path),  file_get_contents($path));  
                } else {
            echo"file does not exist";
            }
        } //End of foreach loop for $files
      } //End of foreach for $myfiledata

      $this->set('ZipName', $zip_name);
      $this->set('ZipFiles', $MyFileData);
      $zip->close();
      copy($zip_name,$UserStartPath.$zip_name);
      unlink($zip_name); //After copy, remove temp file.
      $this->render('/Pages/download');
    } //End of function

私が間違っている場所のアイデアはありますか? これは私のコードではないと述べます。他の投稿でその一部を見つけ、プロジェクトのニーズに合わせて変更しました!

すべてのヘルプ大歓迎...

ありがとう

グレン。

4

1 に答える 1

1

ファイルをメモリにロードすると思うので、php.iniZipArchiveのパラメータを増やす必要があります。 サーバーのすべてのメモリを消費してパフォーマンスを低下させないようにするには、ファイルが大きい場合、より優れた (ただし最善とは言い難い) ソリューションを次のようにする必要があります。 memory_limit

 public function ZippingMyData() {
 $UserStartPath = '/data-files/tmp/';
 $MyFileData = $this->data['ZipData']; //this is the files selected from a form!

 foreach($MyFileData as $DataKey => $DataValue) {
    $files = array($UserStartPath.$DataValue);
    $zip_name = time().".zip"; // Zip name
    // Instead of a foreach you can put all the files in a single command:
    // /usr/bin/zip $UserStartPath$zip_name $files[0] $files[1] and so on
    foreach ($files as $file) {
      $path = $file;
      if(file_exists($path)) {
        exec("/usr/bin/zip $UserStartPath$zip_name basename($path)");  
      } else {
        echo"file does not exist";
      }
    } //End of foreach loop for $files
  } //End of foreach for $myfiledata

  $this->render('/Pages/download');
} //End of function

または同様のもの(サーバーによって異なります)。このソリューションには、ディスク容量と zip の制限の 2 つの制限しかありません。
コードの質が悪く、エラーが発生したことをお詫びします。

于 2013-10-02T16:04:13.360 に答える