我正在使用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
我出错的地方有什么想法吗?我将声明这不是我的代码,我在其他帖子上发现了一些内容,并将其更改为符合我对项目的需求!
欢迎大家帮忙...
由于
格伦。
答案 0 :(得分:1)
我认为ZipArchive
会将您的文件加载到内存中,因此您必须增加php.ini中的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限制 我为我的代码质量差和任何错误道歉。