views:

70

answers:

2

i would like to move the files inside uploads/pension/#SOME_VARIABLE_NUMBER#/#SOME_CONSTANT_NUMBER#/

here is my 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);
}

however... when i need to make the directory with the "mkdir" function, i get:

mkdir() [<a href='function.mkdir'>function.mkdir</a>]: No such file or directory

ok, maybe the mkdir won't work, but what about the rename? perhaps that will make the directory if it's not there... nope!

rename(uploads/pension/1001/783/,uploads/pension/1000/783/) [<a href='function.rename'>function.rename</a>]: The system cannot find the path specified. (code: 3)

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?

thanks!

EDIT: i've modified the code, and now my only issue is an access problem...

rename(uploads/pension/1000_783/,uploads/pension/1001/783/) [<a href='function.rename'>function.rename</a>]: Access is denied. (code: 5)

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?

// 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);
}
A: 

remove the mkdir and only rename:

rename($oldPensionDir, $newPensionDir);

where you always strip down to the directory you want to rename and not its children:

uploads/pension/1001

to

uploads/pension/1000
Tobias
but there are other folders in uploads/pension/1001 that should stay there =(
gsquare567
they will stay - you just rename their parent.
Tobias
A: 

mkdir() has a recursive parameter you can use to create any parent directories needed for the path

baloo
thanks :) the mkdir works now but i have access problems still.
gsquare567
Why is it being renamed like that 3 times?how about if you mkdir() a path excluding $demo_user[Users::userID]. Then rename to that path including $demo_user[Users::userID]
baloo