簡體   English   中英

cakephp 2.3.x或PHP需要重命名目錄中的所有視頻文件-出現錯誤

[英]cakephp 2.3.x or PHP need to rename all video files in a directory - getting error

我在Mac OSX的MAMP服務器上使用CakePHP 2.3.1。 嘗試使用重命名($ oldname,$ newname);時遇到麻煩 因為我總是收到以下錯誤:

致命錯誤

錯誤:已用完33554432字節的允許的內存大小(嘗試分配9662464字節)
文件:/Users/esjenkins/Sites/cakephp/app/View/Layouts/default.ctp
線:56

我嘗試將文件夾的權限設置為所有人讀/寫。

如果我注釋掉重命名($ oldname,$ newname); 我可以像在每個循環中對這兩個變量所期望的那樣得到回聲。 舉個例子:

$ oldname = /用戶/ esjenkins /站點/cakephp/app/webroot/files/asdfasf2929.mp4

$ newname = /用戶/ esjenkins /站點/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 . " <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個裁判文件,上載后需要重命名。

提前致謝。

更新:嘗試在循環中重命名不起作用(根據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 . " <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
}

我不確定是否可以在Controller中包含此代碼,或者將其放入模型中是否合適。 我對CakePHP和編碼一般都是新手。 一旦超過了主要的截止日期,我將回頭嘗試從頭開始學習博客教程,以更好地了解基礎知識。

非常感謝@thaJeztah幫助再次克服了主要障礙。 再次救我的屁股。 非常感謝。

增加php.ini中的memory_limit

您可以通過幾種方式增加內存限制,

  1. 直接php.ini文件,

    memory_limit = 2048M

  2. 通過.htaccess

    php_value memory_limit 2048M

  3. 或使用

    ini_set('memory_limit','2048M');

在您的應用程序中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM