in folder 'original' i have subfolders called 1, 2, 11, 33, 111... they are all numbers
this script sorts array then moves first folder (in this case folder 1) to the folder 'move-here'
everything works ok, BUT it moves 2 folders each time instead of just one, folders 1 & 11 are moved in my first try
echo is giving me 'moved folder: 1', so i really don't know why rename() is moving 2 folders?
$wallpapers = array_map('basename',glob("/path-to-folder/original/*",GLOB_ONLYDIR));
natsort($wallpapers);
$wallpaper = $wallpapers[0];
if (empty($wallpaper)) {
echo "0 folders in that directory";
die();
}
if (!file_exists("/path-to-folder/original/$wallpaper")) {
echo "folder does not exist";
die();
}
echo "moved folder: $wallpaper";
rename("/path-to-folder/original/$wallpaper/","/path-to-folder/move-here/$wallpaper/");
i have faced the similar issue , but i have fixed it using the integer conversion function intval()
. php sorting also have similar issue in some cases.
rename("/path-to-folder/original/$wallpaper/","/path-to-folder/move-here/$wallpaper/");
to
rename("/path-to-folder/original/".intval($wallpaper)."/","/path-to-folder/move-here/".intval($wallpaper)."/");
Your script works fine i just tested it and it only moves one folder. Check if your script is not excuted two times.
<?php
$wallpapers = array_map('basename',glob("./folder1/*",GLOB_ONLYDIR));
natsort($wallpapers);
$wallpaper = $wallpapers[0];
if (empty($wallpaper)) {
echo "0 folders in that directory";
die();
}
if (!file_exists("./folder1/$wallpaper")) {
echo "folder does not exist";
die();
}
echo "moved folder: $wallpaper";
rename("./folder1/$wallpaper/","./folder2/$wallpaper/");
$data = scandir("./folder2/");
echo "<pre>";print_r($data);
Result of scan dir:
Array
(
[0] => .
[1] => ..
[2] => 1
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.