问题描述
所以我试图在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;
}
上面的代码给我一个错误说:
我使用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:
I am using CodeIgniter as framework.
Thank you very much!
If it is on Windows, this can be read in contributions:
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!