Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php rename() Access is denied. (code: 5)

So I am trying to use rename function in php.

On the first try, if the destination folder is empty or does not contain any directories with the same name as the source folder the rename function works perfectly. However, if there is same directory name it fails. What I want is just to overwrite it and I thought rename() would suffice.

Here is my code:

/**
        *   Move temp folders to their permanent places
        *
        *   $module_folder = example (violator, pnp, etc)       
        *   $folders = name of folders within module_folder
        **/
        public function move_temp_to_permanent($module_folder, $folders){
            $bool = false;

            $module_folder_path = realpath(APPPATH . '../public/resources/temps/' . $module_folder);

            $module_folder_destination_path = $_SERVER['DOCUMENT_ROOT'] . '/ssmis/public/resources/photos/' . $module_folder . '/';

            foreach($folders as $folder){
                $bool = rename($module_folder_path . '/' . $folder, $module_folder_destination_path . $folder);
            }

            return $bool;
        }

The code above gives me an error saying:

Message: rename(C:\xampp\htdocs\ssmis\public\resources\temps\violator/SJ-VIOL-2015-0002,C:/xampp/htdocs/ssmis/public/resources/photos/violator/SJ-VIOL-2015-0002): Access is denied. (code: 5)

I am using CodeIgniter as framework.

Thank you very much!

like image 409
iamjc015 Avatar asked May 06 '15 12:05

iamjc015


1 Answers

If it is on Windows, this can be read in contributions:

rename() definitely does not follow the *nix rename convention on WinXP with PHP 5. If the $newname exists, it will return FALSE and $oldname and $newname will remain in their original state. You can do something like this instead:

function rename_win($oldfile,$newfile) {
    if (!rename($oldfile,$newfile)) {
        if (copy ($oldfile,$newfile)) {
            unlink($oldfile);
            return TRUE;
        }
        return FALSE;
    }
    return TRUE;
}

Link.

like image 151
Tpojka Avatar answered Nov 10 '22 11:11

Tpojka