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

我认为这ZipArchive会将您的文件加载到内存中,因此您必须增加memory_limitphp.ini 中的参数。
为了避免消耗服务器的所有内存并降低性能,如果文件很大,更好(但远非最佳)的解决方案应该是:

 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 限制。
对于我的代码质量差和任何错误,我深表歉意。

于 2013-10-02T16:04:13.360 回答