如何在没有文件.(当前目录)或..(父目录)的情况下获取给定目录的所有子目录,然后使用函数中的每个目录?
我有这个函数返回full directory tree:
function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not …Run Code Online (Sandbox Code Playgroud) 我已经能够列出所有文件名和当前目录/文件夹,但我不知道如何为子目录创建 JSON 条目
这是我的代码
<?php
$dir = "office/";
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) != false){
if($file != "." and $file != ".."){
$files_array[] = array('file' => $file); // Add the file to the array
}
}
}
$return_array =array('dir' => $files_array);
exit (json_encode($return_array));
}
?>
Run Code Online (Sandbox Code Playgroud)
和输出是
{
"dir": [
{
"file": "FreeWallpapersApp.zip"
},
{
"file": "20151211_ClipArtForTextView.7z"
},
{
"file": "QRite.7z"
},
{
"file": "CustomDialog_app_contacts.zip"
},
{
"file": "LockScreenBasicApp.apk"
},
{
"file": "ImgViewEffects.zip"
},
]
}
Run Code Online (Sandbox Code Playgroud)
如何使用 php 显示子文件夹中的文件以生成子目录中的文件名。