$dir = "../../";
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) != false){
if($file != "." and $file != ".."){
$files_array[] = array('file' => $file);
}
}
}
sort($files_array);
$return_array =array('name_array' => $files_array);
echo json_encode($return_array);
}
header('Content-type: application/json');
How to make it? There is my source code. I want to know how to sort it + how to echo file extension. Thx:)
Sample:
header('Content-type: application/json'); # before echo
$dir = "../../";
$files_array=[]; # empty array
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) != false){
if($file != "." and $file != ".."){
if(!is_dir($file)){ # check is not dir
$ext = '';
if(strpos($file,'.', 1)) {
$aExt = explode(".", $file);
# select last element for extension
$ext = $aExt[count($aExt)-1];
}
$files_array[] = array('file' => $file, 'ext'=> $ext );
break; # break while
}
}
}
}
# sort($files_array); # sort multiple file
# $return_array =array('name_array' => (isset($files_array[0]?$files_array[0]:[]))); # return only first element if exist
$return_array =array('name_array' => $files_array);
echo json_encode($return_array);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.