php rename()访问被拒绝。 (代码:5) [英] php rename() Access is denied. (code: 5)

查看:694
本文介绍了php rename()访问被拒绝。 (代码:5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在php中使用重命名功能。



在第一次尝试,如果目标文件夹是空的或不包含任何同名的目录作为源文件夹的重命名功能完美地工作。但是,如果存在相同的目录名称,它将失败。我想要的只是覆盖它,我认为rename()就足够了。



这是我的代码:

  / ** 
*将临时文件夹移动到永久位置
*
* $ module_folder = example(violator,pnp等)
* $ folders = 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。
}

return $ bool;
}

上面的代码给我一个错误说:


邮件:
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):
访问被拒绝。 (code:5)


我使用CodeIgniter作为框架。



非常感谢!

解决方案

如果是在Windows上,可以在文件中阅读:



rename()肯定不遵循WinXP与PHP 5的* nix重命名约定。如果$ newname存在,它将返回FALSE,$ oldname和$ newname将保持在其原始状态。您可以这样做:




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

链接


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!

解决方案

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.

这篇关于php rename()访问被拒绝。 (代码:5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆