php - 通过 PHP 脚本从目录中获取所有文件名
问题描述
我在托管在 ftp 服务器上的项目中使用 Angular 2+。在入口处,我想将我的 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;
?>
response
是undefined
或_"Array"
已解决:您无法回显数组
解决方案
您需要使用PHP scandir 函数
例子
<?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
. 如果您print_r
改用它,它将使用上述语法打印它
推荐阅读
- minecraft - 如何在 NPC 上显示第二层皮肤
- api - 通过 API 将用户添加到 Podio 工作区
- data-structures - 我不明白这不是有效的 BST,有人可以解释一下吗?
- android - 如何仅为某些 XML 文件而不是整个项目文件启用 Android ViewBinding
- bluetooth - 设备和服务未定义的 react-native-ble-plx 的特征写入失败
- javascript - 从 Node.js 将 URL 传递到前端
- r - 将矩阵导出到文本文件时添加特殊标题的 R 代码
- php - 如何在 mysql 中将 unix-timestamp 转换为 dmY
- regex - FLEX:匹配正负整数和浮点数的正则表达式是什么,指数也可以存在?
- python - 如何解决在 ES_RNN 模型中运行模型拟合的训练错误?