且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在javascript中显示目录的所有图像?

更新时间:2023-01-06 22:56:30

我不相信这是可能的,但如果你做了对ASP.NET或PHP(或类似)页面的AJAX请求,他们可以列出文件夹中的文件并将其返回给Javascript显示。

I don't believe that it is possible, but if you make an AJAX request to an ASP.NET or PHP (or similar) page, they can list the files in the folder and return it for the Javascript to show.

域策略显然适用。

如果您使用的是PHP,则应在目录上使用递归来获取完整的文件结构。 PHP有一个扫描目录功能,您可以使用。

If you are using PHP, you should use recursion on directories to get the full file structure. PHP has a Scan Directory Function that you can use.

基本代码示例:

function listdir($folder) {
    if (!is_dir($folder)) {
        return array(); //empty if not a folder
    }
    $return = scandir($folder);
    $subfolders = array();
    array_shift($return); //first two values are always .. & .
    array_shift($return);
    foreach ($return as $key => $value) {
        if (is_dir($value)) {
            unset($return[$key]);
            $subfolders[$value] = listdir($value); //recursively analyse the subdirectory
        }
    }
    return array_merge(array_values($return), $subfolders);

}

注意:这还没有经过测试,请告诉我如果它不起作用。

Note: This hasn't been tested, please tell me if it doesn't work.