Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

codeigniter - 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:xampphtdocsssmispublic esourcesempsviolator/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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...