Jump to content

read multiple directories


Kingy

Recommended Posts

I am trying to read all directories within a certain directory and list all the files... the code i have at the moment only lists the main directory.. eg

 

MainDirectory

- dir

- dir

-file

-file

-file

-file

 

what i would like it to do is.

 

MainDirectory

- dir

--file

--file

- dir

--file

--file

-file

-file

-file

-file

Link to comment
https://forums.phpfreaks.com/topic/90985-read-multiple-directories/
Share on other sites

i jumped the gun a bit and forgot to put in my code lol

 

<?php

mysql_connect("localhost", "root", "kwcm92fs");
mysql_select_db("server");

$dir = "./../layout/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if(($file == ".") || ($file == "..")) {
      } else {
        $filetype = explode(".", $file);
        $type = filetype($dir . $file);
        $size = filesize($dir . $file);
        $change = date("d/m/Y H:i:s", filectime($dir . $file));
          echo "<b>filename:</b> $file : <b>type:</b> $type <b>filechanged:</b> $change <br />";
      }
    }
  closedir($dh);
}
}
?>

Make a recursive function.

 

<?php
function readDir($dir)
{
    $items = scandir($dir);
    foreach($items as $item)
    {
         if(in_array($item, array('.', '..'))) continue;
         echo $dir, $item, "\n";
         if(is_dir($item)) readDir($dir . $item);
    }
}

readDir('/home/daniel');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.