【问题标题】:Easiest way to get list of files in the server directory获取服务器目录中文件列表的最简单方法
【发布时间】:2015-08-17 19:08:10
【问题描述】:

我需要获取目录(例如 www.example.com/images/)中所有图像(或简单的所有文件)的数组。我更喜欢使用 JavaScript,但很难做到。那么我应该使用 PHP 吗?

你能帮帮我吗?我不擅长这个。

非常感谢!

【问题讨论】:

标签: javascript php image server


【解决方案1】:

我设法在基本 javascript 中使用修改后的 ajax 命令来获取文件夹列表作为 html 文件。然后我从里面 grep 文件名:

function loadDoc() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    
                    // gets the entire html file of the folder 'logpost' in this case and labels it thing
                    thing = this.responseText
                    searchFor = /.html</g
                    a=0;
                    b=0;
                    var str = "";
            
                    // greps file for .html and then backs up leter by letter till you hot the file name and all
                    while ((dothtmls = searchFor.exec(thing)) != null ){

                        str = "";
                        console.log(dothtmls.index);
                        
                        a = dothtmls.index;

                        while (thing[a]  != '>' ){
                            a--;
                        }
                        a++;
                        while(thing[a] != '<'){
                            str = str + thing[a];
                            a++;
                        }
                        console.log(str);
                    } 
                }
            };
            xhttp.open("GET", "logpost/", true);
            xhttp.send();
            }

这可能不是最干净的方式,但如果您使用的是静态网络服务器,它应该可以工作:)

【讨论】:

  • 很遗憾,由于CORS policy,此代码在 Firefox 和 Chrome 上不起作用
【解决方案2】:

我不同意@mariobgr。如果没有阻止目录列表的服务器设置,则可以解析通过请求该目录生成的 html 的内容。

$ tree maindir
maindir
├── index.html
└── somedir
    ├── doc1
    ├── doc2
    └── doc3

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"><!--  JQUERY  -->

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
  </script>
  <title></title>
</head>
<body>
  <h1>Listing /somedir</h1><!-- Custom Script (defered load, after dom ready) -->
  <script>
    $.getJSON('./somedir', data => {
        console.log(data); //["doc1.jpg", "doc2.jpg", "doc3.jpg"] 
    });
  </script>
</body>

【讨论】:

【解决方案3】:

Javascript 无法获取服务器上的所有文件,因为它是一种客户端语言。

http://php.net/manual/en/function.glob.php 是您所需要的。

$all = glob('/path/to/dir/*.*');

$images = glob('/path/to/dir/*.{jpg,png,gif}');

【讨论】:

  • 嗯,是的,但在这种情况下,node.js 似乎有点过头了。你试过我的建议了吗?
  • 非常感谢。我试过这个,但现在我检测到导致它不起作用的错误。当我在给文本“数组”中使用回声时,我想这很好。现在我只需要遍历这个数组来获取文件名——对吗?
  • 这不起作用。 $images = glob('http://www.honzachalupa.cz/dev/imgs/*.{jpg,png}'); foreach($images as $image) { print $image; }
  • 不要使用域,而是图像目录的路径。例如/home/example.com/public_html/images
  • 即使我使用相对路径并将打印更改为回显它也不起作用。 &lt;?php $images = glob('/imgs/*.{jpg,png}'); foreach($images as $image) { echo $value; } ?&gt;
猜你喜欢
  • 1970-01-01
  • 2010-10-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多