简体   繁体   中英

How Can I filter dir and files inside the parent dir in PHP?

I've a function which scan through the provided dir and returns all the files and sub directory inside the parent dir.

public function show_image(){
        $dir=FCPATH.'uploads/';
        $files = scandir($dir);
        $ret= array();
        $result=array();
        foreach($files as $file)
        {
            if($file == "." || $file == "..")
            continue;
            $filePath=$dir."/".$file;

            $details = array();
            $details['name']=$file;
            $details['path']=$filePath;
            $details['size']=filesize($filePath);
            $ret[] = $details;
    }

    echo json_encode($ret);
    //echo json_encode($result);
    }

Basically I'm using Ajax, so what I'm doing is printing both folders and files inside that dir. But the problem here is that I really want to filter the subdir and file which this function isn't currently doing. I want to have the folder printed out at the very beginning whereas other files after the folders. The $ret consists the data in ascending order. In the view I've following Ajax onSucess function.

 onSuccess:function(files,data,xhr,pd)
{

        var src = '<?php echo base_url("uploads"); ?>'+'/'+data.file_name;
        var html = '<div class="col-sm-3 text-center"><div class="mid-folder">'; 
          html+=    '<div class="folder-content"><img src="'+src+'" class="img-container" style="max-height:100%; max-width:100%;"/></div>'
          html+=    '<a href="#" class="link-folder" title="link folder"><h3 class="title-folder">'+data.file_name.substr(0,10)+".."+' </h3></a> </div>';
          $('.hello').append(html);
          $('.ajax-file-upload-statusbar').fadeOut(1000, function(){
              $('.ajax-file-upload-statusbar').show(1000).html('Your file successfully uploaded');
          });

          $('.ajax-file-upload-statusbar').hide('slow');
}

What should I be doing, so that I could display the folder and files in different way. Basically a logic/way by which I can filter which object inside the $ret should be treated as dir and which as file and display it thoroughly.

I'd go with separering files and directories on the server side, and then sending a json object with to arrays. One with directories and one with files.

$ret= array();
$result=array();
foreach($files as $file){
    if($file == "." || $file == "..")
        continue;

    $filePath=$dir."/".$file;
    $details = array();
    $details['name']=$file;
    $details['path']=$filePath;
    $details['size']=filesize($filePath);
    if(is_dir($filePath) {
        $ret['directories'][] = $details;
    }
    else {
        $ret['files'][] = $details;
    }
}

Next, make two loops in the ajax success callback function. One for data.directories and one for data.files .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM