2 回答
TA贡献1876条经验 获得超7个赞
而不是使用 ,获取目录中所有路径的唯一方法是使用PHP循环文件并将输出返回为JSON或XML(或者可能是文本,但随后您必须解析它),JSON是您的情况下要走的路,您所要做的就是使用Ajax请求此php脚本的路径:var_dump()json_encode(getDirContents('directory'))
<?php
function getDirContents($dir, &$results = array()){
$files = array_diff(scandir($dir), array('..', '.'));;
foreach($files as $key => $value){
$path = $dir.DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
getDirContents($path, $results);
} else {
// change this to https if you have HTTPS
// or have it as a variable like this :
// $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$directory_path = basename($_SERVER['REQUEST_URI']);
$results[] = 'http://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
}
}
return $results;
}
echo json_encode(getDirContents('directory'));
对于阿贾克斯呼叫:
$(document).ready(function() {
$.ajax({
type: 'POST',
url: '../test.php',
data: 'id=testdata',
dataType: 'json',
cache: false,
success: function(result) {
console.log(result)
},
});
});
TA贡献1812条经验 获得超5个赞
这有点太晚了,但也许对于将来的某个人来说。
我用这个JS代码得到了我所有的文件路径。
let htmlContainer = document.createElement('div');
fetch("http://192.168.0.9:5500/assets/img/gallery/project-1", {
headers : {
'Content-Type': 'text/html',
'Accept': 'text/html',
}
}).then(response => response.text())
.then((stringHtml) => {
htmlContainer.innerHTML = stringHtml; //convert plain text to Html to have
//acces to its elements
//htmlContainer will containe html representation
//of our project diretories and files, so from there
//will be able to get files paths
console.log( Array.from(htmlContainer
.querySelector('#files')
.querySelectorAll("li>a"))
.map( x => x.getAttribute('href'))) //will print an array
//with all file and direcotory paths in our directory
})
- 2 回答
- 0 关注
- 319 浏览
添加回答
举报