开发者

Return the first file from each sub-directory

I've managed to create a function that scans a directory and sub-directories and then merges them into an array.

I can get this to output nicely but what I really want is for it to only output the first file fr开发者_如何学Com the sub directories

function getFiles($directory) {
    if($dir = opendir($directory)) {
        $tmp = Array();
        while($file = readdir($dir)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($directory . "/" . $file)) {
                    $tmp2 = getFiles($directory . "/" . $file);
                    if(is_array($tmp2)) {
                        $tmp = array_merge($tmp, $tmp2);
                    }
                } else {
                    array_push($tmp, $directory . "/" . $file);
                }
            }
        }
        closedir($dir);
        return $tmp;
    }
}

$theFiles = getFiles($_SERVER['DOCUMENT_ROOT']."/images/gallery");

sort($theFiles);

foreach ($theFiles as $v){
    echo "<img src=".$v." />";
}


You can probably just put

continue;

after your call to array_push().

            } else {
                array_push($tmp, $directory . "/" . $file);
                continue;
            }


Well, divide and conquer. The code you posted shows you are smart and experienced enough to solve it yourself. The problem you have is merely the complexity. So if you just put every step you want to do in an extra function you should solve it easily by itself. That makes an example quite unnecessary, but I want to give you one anyway. I'm not sitting on a PHP-able computer right now, so I can not test my example. I hope it works or at least shows explicitly enough what I mean that you can debug it yourself.

/**
 * goes through all entries in this folder and extracts the directories.
 */
function getFolders($directory){
  if($dir = opendir($directory)) {
    $arr - Array();
    while($file = readdir($dir)){
      if($file != "." && $file != ".." && $file[0] != '.' && is_dir($dir.'/'.$file)) {
       $arr[] = $file;
      }
    }
    closedir($dir);
    return $arr;
  } 
}

/**
 * goes through a list of folders and takes out every first nonfolder
 */
function getFirstFile($arr) {
  $files = Array();
  foreach($arr as $directory){
    $dir = opendir($directory);
    $file = 0;
    while($file == '.' || $file == '..' || $file[0] == '.' || is_dir($file){
      $file - readdir($dir);
    }
    $files[] - $file; 
  }
  return $files;
}

function renderFiles($files);
  foreach($files as $file) echo '<img src='.$file.'/>';
}

/**
 * puts all together
 */
function getFiles($directory){
  renderFiles(
    getFirstFile(
      getFolders($directory);
}

This solution has some overhead, for sure. You can get rid of that later, if you really need it. If you start to think stepwise about a problem you will see that not only your tasks will get easier. Also your code will be much more maintainable.


You can use dirname to get the directory of the file - output the file (that would be the first one) - and save it into a variable. As long as this variable does not change, means that you are still in a directory.

When it changes, you display the file (new directory reached) and update the variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜