我在CI控制器中有一个数组,如下所示
Array
(
[user_id] => 109676506743696803215
[sub_dirs] => Array
(
[0] => Array
(
[legacyListID] => 00984bc3-aaa3-4519-be03-09a73623f2b1
[slug] => st-peters-model-school
)
[1] => Array
(
[legacyListID] => 092452b5-3bc1-4f8e-96d4-dfb96e3af9fc
[slug] => sri-aurobindo-international-school
)
[2] => Array
(
[legacyListID] => 0d65157d-9bf6-44e9-9b72-ab9e37faa130
[slug] => suprabhat-model-high-school
)
[3] => Array
(
[legacyListID] => 20358e6f-7b15-4efb-b259-e4a34fea37ad
[slug] => rushi-high-school
)
[4] => Array
(
[legacyListID] => 262c1151-d39c-4ae4-b9c9-f68fa606fda3
[slug] => vignan-high-school
)
[5] => Array
(
[legacyListID] => 5d064b73-97f0-43b6-8aa6-da98cbca646e
[slug] => smart-kidz-play-school
)
[6] => Array
(
[legacyListID] => 68015e74-bba8-460a-93f7-282c3bf7ddb8
[slug] => gurukila-vidyapeeth-high-school
)
[7] => Array
(
[legacyListID] => 76ff39b9-e9a4-4b2b-bc8b-36e11089e3e7
[slug] => the-mothers-integral-school
)
[8] => Array
(
[legacyListID] => a31c7919-2007-4f83-8674-57010c43520f
[slug] => sr-digi-school
)
[9] => Array
(
[legacyListID] => cf078592-d7ce-46f6-b18c-8c6ead70f1d8
[slug] => gautami-talent-school
)
[10] => Array
(
[legacyListID] => eccebdea-8db9-4f45-8016-235a6a03ff6f
[slug] => anglist-high-school
)
[11] => Array
(
[legacyListID] => fdc44658-bed0-458e-85b0-6897d1175a93
[slug] => pratibha-high-school
)
)
这里user_id是文件夹名称,我在文件夹中有sub_dirs。 sub_dirs变量包含在user_id文件夹中具有实际子目录名称的数据数组,以及用于重命名它的重命名参数(slug)。
我在做如下
foreach ($dirs as $dir) {
$dirPath='images/profile/'.$dir['user_id'];
if(is_dir($dirPath)){
foreach ($dir['sub_dirs'] as $sub) {
$subDir=$dirPath."/".$sub['legacyListID'];
if(is_dir($subDir)){
rename($subDir,$sub['slug']);
}
}
}
}
我的操作系统是Windows。如果我错过了许可问题或者在codeigniter中这样做的帮助,你能帮助我吗?
仅供参考: images / profiles /(dir)/(subDir)位于根文件夹中
答案 0 :(得分:0)
假设您的images文件夹位于应用程序路径中,您应该使用APPPATH
CI变量:
FCPATH -> '/'
BASEPATH -> '/system/'
APPPATH -> '/application/'
答案 1 :(得分:0)
将FCPATH用于目录位置
foreach ($dirs as $dir) {
$dirPath = FCPATH . 'images/profile/'. $dir['user_id'];
if(is_dir($dirPath)){
foreach ($dir['sub_dirs'] as $sub) {
$subDir = $dirPath ."/". $sub['legacyListID'];
if(is_dir($subDir)){
rename($subDir, $sub['slug']);
}
}
}
}
确保您的images文件夹位于主目录
中application
images >
images > profile
您也可以使用dirname(FCPATH) . '/images/profile/'. $dir['user_id'];
答案 2 :(得分:0)
使用PHP中的scandir
函数解决它,如下所示
foreach ($dirs as $dir) {
$path="images/profiles";
$curDir=$path."/".$dir['user_id'];
if(is_dir($curDir)){
foreach ($dir['sub_dirs'] as $i => $sub) {
foreach (scandir($curDir) as $subDir) {
if($subDir == $sub['legacyListID']){
$old=$curDir."/".$sub['legacyListID'];
$rename=$curDir."/".$sub['slug'];
rename($old, $rename);
}
}
}
}
}
基本上dirs是实际的数组,它包含所有目录和子目录,并在其中重命名数组。
所以我扫描了每个目录并获得了所有子目录,因此每当有匹配时我都会更名。
理解重命名不会与for循环一起工作。我们首先在目录中获取所有子目录并与您自己的数组匹配,然后重命名它。
ALL GOOD:)