通过PHP脚本从目录中获取所有文件名

时间:2019-07-03 11:21:34

标签: php directory scandir

我将Angular 2+用于我的项目,该项目托管在ftp服务器上。在入口处,我想将我的ftp服务器上某个目录的所有文件名加载到数组中,然后将其返回给Typescript。到目前为止,这是我的代码:

文件夹结构:

-scripts
   - getMasterplan.php
-shared_plaene

TS:

    let http = new XMLHttpRequest()
    http.open('POST', Globals.PATH + '/scripts/getMasterplan.php', true)

    // http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    let resp
    http.onreadystatechange = ()=> {

        if(http.readyState == 4 && http.status == 200)
            resp = http.responseText

        console.log('resp',resp)
    }

    http.send(null)

PHP:

<?php

  session_start();

  $files = array();

  $rootDir = "../shared_plaene";

  $path = $rootDir;
  $files = scandir($path);

  echo $files;

?>

responseundefined"Array"

已解决:您无法回显数组

1 个答案:

答案 0 :(得分:2)

您需要使用PHP scandir function

示例

EXPOSE 8080

您不能<?php $dir = "/images/"; // Sort in ascending order - this is default $a = scandir($dir); // Sort in descending order $b = scandir($dir,1); print_r($a); //Array ( [0] => . [1] => .. [2] => cat.gif [3] => dog.gif [4] => horse.gif [5] => myimages ) echo ($a[4]); // horse.gif ?> echo。如果您使用Array,它将使用上面的语法进行打印