title: PHP: How do I rename a directory where the parent directory is variable?
body: <p>i would like to move the files inside uploads/pension/#SOME_VARIABLE_NUMBER#/#SOME_CONSTANT_NUMBER#/</p>
  
  <p>here is my code:</p>
  
  <pre><code>// move pension statements
  // located at uploads/pension/%COMPANY_ID%/%USER_ID%/%HASH%
  // so just move the %USER_ID% folder to the new company
  $oldPensionDir = "uploads/pension/" . $demo_user[Users::companyID]  . "/" . $demo_user[Users::userID] . "/";
  $newPensionDir = "uploads/pension/" . $newCompanyID      . "/" . $demo_user[Users::userID] . "/";
  // see if the user had any files, and if so, move them
  if(file_exists($oldPensionDir)) {
   // if it doesnt exist, make it
   if(!file_exists($newPensionDir))
    mkdir($newPensionDir);
   // move the folder
   rename($oldPensionDir, $newPensionDir);
  }
  </code></pre>
  
  <p>however... when i need to make the directory with the "mkdir" function, i get:</p>
  
  <pre><code>mkdir() [&lt;a href='function.mkdir'&gt;function.mkdir&lt;/a&gt;]: No such file or directory
  </code></pre>
  
  <p>ok, maybe the mkdir won't work, but what about the rename? perhaps that will make the directory if it's not there... nope!</p>
  
  <pre><code>rename(uploads/pension/1001/783/,uploads/pension/1000/783/) [&lt;a href='function.rename'&gt;function.rename&lt;/a&gt;]: The system cannot find the path specified. (code: 3)
  </code></pre>
  
  <p>so, there are two errors. i'm pretty sure if the renaming works, i won't even need the mkdir, but who knows... can anyone tell me why these are errors and how to fix them?</p>
  
  <p>thanks!</p>
  
  <p>EDIT: i've modified the code, and now my only issue is an access problem...</p>
  
  <pre><code>rename(uploads/pension/1000_783/,uploads/pension/1001/783/) [&lt;a href='function.rename'&gt;function.rename&lt;/a&gt;]: Access is denied. (code: 5)
  </code></pre>
  
  <p>below is the new code. basically, i rename it three times (because it has to move through folders, but the final move is what causes the 'access denied' error. the odd part is that even when i delete the new dir and it makes a new one, i set it to have perms 0777!!! whats wrong with this?</p>
  
  <pre><code>// move pension and total reward statements
  // located at uploads/pension|total_rewards/%COMPANY_ID%/%USER_ID%/%HASH%
  // so just move the %USER_ID% folder to the new company
  $oldPensionDir = "uploads/pension/" . $demo_user[Users::companyID]   . "/" . $demo_user[Users::userID] . "/";
  $tempPensionDir1 = "uploads/pension/" . $demo_user[Users::companyID]     . "/" . $demo_user[Users::companyID] . "_" . $demo_user[Users::userID] . "/";
  $tempPensionDir2 = "uploads/pension/" . $demo_user[Users::companyID]     . "_" . $demo_user[Users::userID] . "/";
  $newPensionDir = "uploads/pension/" . $newCompanyID                  . "/" . $demo_user[Users::userID] . "/";
  // see if the user had any files, and if so, move them
  if(file_exists($oldPensionDir)) {
      // if it doesnt exist, make it
      if(!file_exists($newPensionDir))
          mkdir($newPensionDir, 0777, true);
      // move the folder
      // first, move it to the pension directory
      rename($oldPensionDir, $tempPensionDir1);
      rename($tempPensionDir1, $tempPensionDir2);
      // second, move it into the new directory
      rename($tempPensionDir2, $newPensionDir);
  }
  </code></pre>