编写了一个在构建的php函数DirectoryIterator中使用的类 其中显示像这样的文件夹结构
root
|__subfolder
| |_mp3
| |_png
|__subfolder2
|_mp3
|_mp3
这是我的班级
<?php
$list = array();
$dir = new DirectoryIterator('/opt/lampp');
foreach ($dir as $fileinfo)
{
if ($fileinfo->isFile())
{
//echo $fileinfo->getBasename() . "\n";
//echo $fileinfo->getBasename('.jpg') . "\n";
}
else
{ //create object with two fields
$list3 = array( 'name'=> $fileinfo->getFilename(),
'type'=> $fileinfo->getType(),
'date'=> $fileinfo->getMTime(),
'size'=> $fileinfo->getSize());
array_push($list, $list3);
}
$return_array = array('files'=>$list);
echo json_encode( $return_array)
}
?>
编写了echo json_encode,但字符串格式似乎没有正确写入
这里是输出的字符串格式的视图,我使用在线json查看器进行验证
{"files":[{"name":"info","type":"dir","date":1491647493,"size":4096}]} {"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096}]}{"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096},
答案 0 :(得分:0)
使用json_encode()
<?php
$list = array();
$dir = new DirectoryIterator('/opt/lampp');
foreach ($dir as $fileinfo)
{
if ($fileinfo->isFile())
{
//echo $fileinfo->getBasename() . "\n";
//echo $fileinfo->getBasename('.jpg') . "\n";
}
else
{ //create object with two fields
$list3 = array( 'name'=> $fileinfo->getFilename(),
'type'=> $fileinfo->getType(),
'date'=> $fileinfo->getMTime(),
'size'=> $fileinfo->getSize());
array_push($list, $list3);
}
$return_array = array('files'=>$list);
echo json_encode($return_array);
}
?>