MacOSXのMAMPサーバーでCakePHP2.3.1を使用しています。rename($ oldname、$ newname);を使おうとすると問題が発生します。常に次のエラーが発生するためです。
致命的な誤り
エラー:許可されたメモリサイズ33554432バイトが使い果たされました(9662464バイトを割り当てようとしました)
ファイル:/Users/esjenkins/Sites/cakephp/app/View/Layouts/default.ctp
行:56
フォルダーのアクセス許可をすべての読み取り/書き込みに設定してみました。
rename($ oldname、$ newname);をコメントアウトすると 各ループのこれら2つの変数に期待するように、エコーを取得できます。例として:
$ 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 . " <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のアドバイスによる)。rename($ 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
}
このコードをコントローラーに入れても大丈夫なのか、それともモデルに入れるのが適切なのかはわかりません。私はCakePHPとコーディング全般にとても慣れていません。主要な締め切りを過ぎたらすぐに戻って、ブログチュートリアルを最初から学習して、基本をよりよく理解してみます。
再び大きなハードルを乗り越えるのを手伝ってくれた@thaJeztahに大いに感謝します。もう一度私のお尻を保存します。どうもありがとう。