我在Mac OSX上的MAMP服务器上使用CakePHP 2.3.1 . 我尝试使用重命名时遇到麻烦($ oldname,$ newname);因为我总是得到以下错误:
致命错误
错误:允许的内存大小为33554432字节耗尽(尝试分配9662464字节)
文件:/Users/esjenkins/Sites/cakephp/app/View/Layouts/default.ctp
行:56
我尝试将文件夹的权限设置为每个人都读/写 .
如果我注释掉重命名($ oldname,$ newname);我可以得到一个回声,就像我期望每个循环中的这两个变量一样 . 举个例子:
$ oldname = /Users/esjenkins/Sites/cakephp/app/webroot/files/asdfasf2929.mp4
$ newname = /Users/esjenkins/Sites/cakephp/app/webroot/files/2929.mp4
以下是我的View文件夹文件中名为list_adjudication.ctp的PHP代码
<div class="dances index">
<h3><?php echo 'Adjudication Files'; ?></h3>
<?php
$adjudication_path = APP . 'webroot/files/';
if ($handle = opendir($adjudication_path)) {
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != ".DS_Store" && $entry != "empty") {
$adj_file_string = "$entry\n";
//echo $adj_file_string . "
";
$rename_adj_file_string = substr($adj_file_string, -9, 8);
//echo $rename_adj_file_string . "
";
$oldname = $adjudication_path . $adj_file_string;
$newname = $adjudication_path . $rename_adj_file_string;
echo $oldname . " | " . $newname . "
";
rename($oldname, $newname);
}
}
closedir($handle);
}
?>
我有大约750个裁决文件,我需要在上传后重命名 .
提前致谢 .
更新:尝试在循环中重命名不起作用(根据下面的thaJeztah的建议) . 我能够成功使用重命名($ oldname,$ newname);循环之外 . 我将更多地尝试将文件名放入数组中,稍后我将报告结果 .
答案:我无法让rename()在任何循环中工作,即使出于某种原因使用数组也是如此 . 但是我确实通过将这些代码放入控制器来实现工作:
public function list_adjudication() {
$adjudication_path = APP . 'webroot/files/';
$dir = new Folder($adjudication_path);
$files = $dir->find('.*\.mp4');
//print_r($files);
foreach ($files as $file) {
//$file = new File($dir->pwd() . DS . $file);
echo $file . "
";
//$contents = $file->read();
// // $file->write('I am overwriting the contents of this file');
// // $file->append('I am adding to the bottom of this file.');
// // $file->delete(); // I am deleting this file
$newName = substr($file, -8, 8);
//echo $newName; exit;
rename($adjudication_path . $file, $adjudication_path . $newName);
}
//$file->close(); // Be sure to close the file when you're done
}
我不确定是否可以在Controller中使用此代码,或者将它放在模型中是否合适 . 我是CakePHP和编码的新手 . 一旦我超过一个主要的截止日期,我将回去尝试从头开始学习博客教程,以便更好地掌握基础知识 .
非常感谢@thaJeztah帮助AGAIN克服了一个重大障碍 . 再次拯救我的屁股 . 非常感谢 .
1 回答
增加php.ini中的memory_limit
你可以通过几种方式增加内存限制,
memory_limit = 2048M
php_value memory_limit 2048M
ini_set('memory_limit','2048M');
在你的申请中 .