Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对文件夹名称数组进行排序以显示文件夹的内容_Php - Fatal编程技术网

Php 对文件夹名称数组进行排序以显示文件夹的内容

Php 对文件夹名称数组进行排序以显示文件夹的内容,php,Php,我编写了一个函数,用于获取文件夹的所有子文件夹。它还可以选择向上移动并显示文件夹…很像文件管理器,但它仅适用于文件夹,并且有一个限制,即不能向上移动到基本文件夹,以防止用户浏览整个硬盘 我将结果作为json对象返回,并放入div中。我的问题是: 在“我的windows”框中(使用wamp),文件夹“上一级”始终显示在文件夹列表的顶部。当我把它上传到我的网站上时,它不会显示在顶部,而是在中间的某个地方。我已经编写了一个函数(fixArray($array))来纠正这个问题……但它似乎不起作用。提供

我编写了一个函数,用于获取文件夹的所有子文件夹。它还可以选择向上移动并显示文件夹…很像文件管理器,但它仅适用于文件夹,并且有一个限制,即不能向上移动到基本文件夹,以防止用户浏览整个硬盘

我将结果作为json对象返回,并放入div中。我的问题是: 在“我的windows”框中(使用wamp),文件夹“上一级”始终显示在文件夹列表的顶部。当我把它上传到我的网站上时,它不会显示在顶部,而是在中间的某个地方。我已经编写了一个函数(fixArray($array))来纠正这个问题……但它似乎不起作用。提供了此功能的完整解决方案,因此请帮助使其工作,并让其他人从中受益。 只需提及:JS代码中没有任何错误。JSON对象按照php文件发送的顺序进行解析

function fixPath($path) {
    $path = str_replace('/../', '/', $path);
    $path = str_replace('/./', '/', $path);
    return $path;
}
function fixArray($array) {
    for($i = 0; $i < count($array); $i++) {
        if(!strcmp($array[$i]['name'],"Up one Level")) {
            $aux = $array[$i];
            $array[$i] = $array[0];
            $array[0] = $array[$i];
            break;
        }
    }
}

function directoryList($basepath, $path) {
        $dirStruct = array();
        if(is_dir($path)) {
            $handle = opendir($path);
            while(($file = readdir($handle)) !== false) {
                if($file != '.') {
                    if(@opendir($path.$file)) {
                        $newpath = "";
                        if($file == '..') {//begin constructing the path for the upper level
                            $array = @split('/',$path);

                            $up = "";
                            for($i = 0; $i < count($array) - 2; $i++) {
                                $up = $up.$array[$i].'/';
                            }
                            $file = "Up one Level";
                            //if the upper level exceeds the home dir
                            if(strcmp($up,$basepath) < 0) {
                                $newpath = $basepath;//use the basepath
                            }
                            else {
                                $newpath = $up;
                            }
                        }
                        else {//construct the path for a normal dir
                            $newpath = $path.$file.'/';
                            $newpath = fixPath($newpath);
                        }
                        $dirStruct[] = array('path' => $newpath, 'name'=>$file);
                    }
                }
            }
        }
        //sortArray($dirStruct);
        fixpath($dirStruct);
        return $dirStruct;
    }
函数fixPath($path){
$path=str_replace('/../'、'/'、$path);
$path=str_replace('/./','/',$path);
返回$path;
}
函数fixArray($array){
对于($i=0;$i$newpath,'name'=>$file);
}
}
}
}
//索塔雷($dirStruct);
fixpath($dirStruct);
返回$dirStruct;
}

您需要通过引用传递它以保留更改:)

函数fixArray(&$array){
对于($i=0;$i
您的交换是先交换,然后再交换回来<代码>$array[0]=$array[$i]应该是
$array[0]=$aux

但这将把原始的第一个元素交换到数组的中间。只需将目标元素移动到数组的前面即可

试试这个:

function fixArray($array) {
    for($i = 0; $i < count($array); $i++) {
        if($array[$i]['name'] === "Up one Level") {
            $aux = $array[$i];
            array_unshift($array, $aux);
            unset($array[$i]);
            break;
        }
    }
    return $array;
}

不会工作,不会通过引用传递或返回新数组-实际上它只是运行,然后所有修改都会被忘记;)
function fixArray($array) {
    for($i = 0; $i < count($array); $i++) {
        if($array[$i]['name'] === "Up one Level") {
            $aux = $array[$i];
            array_unshift($array, $aux);
            unset($array[$i]);
            break;
        }
    }
    return $array;
}
$dirStruct = fixpath($dirStruct);