我正在使用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
知道我哪里出错了吗?我会声明这不是我的代码,我在其他人的帖子中找到了它的一部分并对其进行了更改以满足我的项目需求!
所有帮助最欢迎...
谢谢
格伦。
我认为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 限制。
对于我的代码质量差和任何错误,我深表歉意。