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
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);
}
}
?>

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.