- for($i = $id+1; $i < count($db_connections); $i++) {
- if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
+ // rename directory to temporary name to ensure all
+ // other subdirectories will have right owners even after
+ // unsuccessfull removal.
+ $cdir = company_path($id);
+ $tmpname = company_path('/old_'.$id);
+ if (!@rename($cdir, $tmpname)) {
+ display_error(_('Cannot rename subdirectory to temporary name.'));
+ return;
+ }
+ // 'shift' company directories names
+ for ($i = $id+1; $i < count($db_connections); $i++) {
+ if (!rename(company_path($i), company_path($i-1))) {