php rename()正在移动两个文件夹而不是一个

时间:2013-10-15 02:33:49

标签: php rename move

在'original'文件夹中我有一个名为1,2,11,33,111的子文件夹......它们都是数字

此脚本对数组进行排序,然后将第一个文件夹(在本例中为文件夹1)移动到文件夹“move-here”

一切正常,但它每次移动2个文件夹而不只是一个文件夹1和文件夹1我第一次尝试移动了11个

echo给我'移动文件夹:1',所以我真的不知道为什么rename()正在移动2个文件夹?

$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/");

2 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,但我使用整数转换函数intval()修复了它。在某些情况下,php排序也有类似的问题。

rename("/path-to-folder/original/$wallpaper/","/path-to-folder/move-here/$wallpaper/");

rename("/path-to-folder/original/".intval($wallpaper)."/","/path-to-folder/move-here/".intval($wallpaper)."/");

答案 1 :(得分:0)

你的脚本工作正常,我只是测试它,它只移动一个文件夹。 检查您的脚本是否两次都不会被执行。

<?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);

扫描目录的结果:

   Array
    (
        [0] => .
        [1] => ..
        [2] => 1
    )