title: cakephp 2.3.x or PHP need to rename all video files in a directory - getting error
body: <p>I'm using CakePHP 2.3.1 on a MAMP server on Mac OSX.  I am having trouble when I try to use rename($oldname, $newname); because I always get the following error:</p>
  
  <p>FATAL ERROR</p>
  
  <p>Error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9662464 bytes)<br>
  File: /Users/esjenkins/Sites/cakephp/app/View/Layouts/default.ctp<br>
  Line: 56</p>
  
  <p>I tried setting the permissions on the folder to everyone read/write.  </p>
  
  <p>If I comment out the rename($oldname, $newname); I can get an echo as I would expect for those two variables in each loop.  As an Example:</p>
  
  <p>$oldname = /Users/esjenkins/Sites/cakephp/app/webroot/files/asdfasf2929.mp4</p>
  
  <p>$newname = /Users/esjenkins/Sites/cakephp/app/webroot/files/2929.mp4</p>
  
  <p>Here is the PHP code in my View folder file called list_adjudication.ctp</p>
  
  <pre><code>&lt;div class="dances index"&gt;
      &lt;h3&gt;&lt;?php echo 'Adjudication Files'; ?&gt;&lt;/h3&gt;
      &lt;?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 != "." &amp;&amp; $entry != ".." &amp;&amp; $entry != ".DS_Store" &amp;&amp; $entry != "empty") {
              $adj_file_string = "$entry\n"; 
                  //echo $adj_file_string . " &lt;br /&gt; &lt;br /&gt; ";
                  $rename_adj_file_string = substr($adj_file_string, -9, 8);
                  //echo $rename_adj_file_string . " &lt;br /&gt; &lt;br /&gt; ";
                  $oldname = $adjudication_path . $adj_file_string;
                  $newname = $adjudication_path . $rename_adj_file_string;
                  echo $oldname . " | " . $newname . " &lt;br /&gt; &lt;br /&gt; "; 
                  rename($oldname, $newname);
              }
          }
  
          closedir($handle);
      }
      ?&gt;
  </code></pre>
  
  <p>I have about 750 adjudication files that I will need renamed after upload.</p>
  
  <p>Thanks in advance.</p>
  
  <p>UPDATE: Trying to rename in a loop doesn't work (per thaJeztah's advice below).  I was able to successfully use the rename($oldname, $newname); outside of the loop.  I will experiment more with putting the file names into an array and I will report the results later.  </p>
  
  <p>ANSWER: I couldn't get rename() to work in any loop, even with the use of an array for some reason.  But I did get things working by putting this code into the controller:</p>
  
  <pre><code>public function list_adjudication() {
      $adjudication_path = APP . 'webroot/files/';
      $dir = new Folder($adjudication_path);
      $files = $dir-&gt;find('.*\.mp4');
      //print_r($files);
      foreach ($files as $file) {
          //$file = new File($dir-&gt;pwd() . DS . $file);
          echo $file . " &lt;br /&gt; ";  
          //$contents = $file-&gt;read();
          // // $file-&gt;write('I am overwriting the contents of this file');
          // // $file-&gt;append('I am adding to the bottom of this file.');
          // // $file-&gt;delete(); // I am deleting this file
      $newName = substr($file, -8, 8);
      //echo $newName; exit;
      rename($adjudication_path . $file, $adjudication_path . $newName); 
      }
          //$file-&gt;close(); // Be sure to close the file when you're done
  }
  </code></pre>
  
  <p>I'm not sure if it's ok to have this code in the Controller or if it would be proper to put it in the Model.  I'm super new to CakePHP and coding in general.  As soon as I get past a major deadline I'm going to go back and try learning the blog tutorial from scratch to get a better grasp of the basics.</p>
  
  <p>HUGE THANKS to @thaJeztah for helping to AGAIN get over a major hurdle.  Save my butt again.  Many thanks.</p>