0
Greetings!
I am developing a file system to read the files of a given directory and return the value in JSON. However, I intend to separate in this JSON if the item is a file ("type":"file"
) or a folder("type":"folder"
).
For that, I’m using the function is_dir()
(tried also with the function is_file()
). Maass... Didn’t work, getting the following result:
The code I used:
<?php
header('Content-Type: application/json');
$path = str_replace("\\", "/", './root');
$dir = dir($path);
$json = array();
while($file = $dir -> read()){
if ($file != ".." && $file != "."){
array_push(
$json,
array(
'path'=>$path.'/'.$file,
'file'=>$file,
'type'=>(!is_dir($file)) ? 'file' : 'folder'
)
);
}
}
echo json_encode($json);
?>
NOTE: I also tested using
dirname(__FILE__)
in place of./
in the variable$path
(so it’s inside thestr_replace()
)– Nary Luiz