cakephp 2.3.x或PHP需要重命名目录中的所有视频文件-获取错误



我在Mac OSX的MAMP服务器上使用CakePHP 2.3.1。我有麻烦,当我试图使用重命名($oldname, $newname);因为我总是得到以下错误:

致命错误

错误:允许的内存大小为33554432字节耗尽(试图分配9662464字节)
文件:/用户/esjenkins/网站/cakephp/app/视图/布局/default.ctp
线:56

我尝试将文件夹的权限设置为每个人读/写。

如果我注释掉rename($oldname, $newname);在每个循环中,我可以得到两个变量的回显。示例:

$oldname =/Users/esjenkins/Sites/cakephp/app/webroot/files/asdfasf2929.mp4

$newname =/Users/esjenkins/Sites/cakephp/app/webroot/files/2929.mp4

下面是我的视图文件夹文件list_adjudicationctp中的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 = "$entryn"; 
                //echo $adj_file_string . " <br /> <br /> ";
                $rename_adj_file_string = substr($adj_file_string, -9, 8);
                //echo $rename_adj_file_string . " <br /> <br /> ";
                $oldname = $adjudication_path . $adj_file_string;
                $newname = $adjudication_path . $rename_adj_file_string;
                echo $oldname . " | " . $newname . " <br /> <br /> "; 
                rename($oldname, $newname);
            }
        }
        closedir($handle);
    }
    ?>

我有大约750个裁决文件,我需要在上传后重新命名。

提前感谢。

更新:尝试在循环中重命名不工作(根据jeztah的建议如下)。我能够成功地使用重命名($oldname, $newname);在循环之外。我将更多地尝试将文件名放入数组中,并在稍后报告结果。

ANSWER:由于某种原因,我无法使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 . " <br /> ";  
        //$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
}

我不确定是否可以在控制器中使用此代码,或者将其放入模型中是否合适。我是CakePHP和编码的新手。一旦过了一个重要的截止日期,我就会回去从头开始学习博客教程,以便更好地掌握基础知识。

非常感谢@thaJeztah再次帮助我们克服了一个主要障碍。再救我一次。多谢。

在php.ini中增加memory_limit

您可以通过几种方式增加内存限制,

  1. 直接php.ini文件,

    memory_limit = 2048M

  2. via .htaccess

  3. 或使用

    报错(' memory_limit ', ' 2048 ');

最新更新