Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

I also want to read the folders and put the folders in a separate array

this is for php on windows os


<?php

$filesarray = array();
$folder_location='C:/Users/Acer/Documents/';
if(!is_readable($folder_location))
{
  echo "You don't have enough permissions to access this folder!";
  die();
}

if ($handle = opendir($folder_location)){
    echo "Opened the folder successfully!<br>\n";
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($folder_location.''.$entry)) {
            echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}
else
{
  echo "There was an error opening the folder!";
  die();
}

print_r($filesarray);
?>

Open in new window



this reads all the files and puts the files in an array

but I also want to read the folders and put the folders in a separate array

example of folders:
C:/Users/Acer/Documents/pictures/
C:/Users/Acer/Documents/excel/
C:/Users/Acer/Documents/word/
C:/Users/Acer/Documents/notepad/
Avatar of Rahul Gupta
Rahul Gupta
Flag of India image

have a look, this function works for me. you can use different array for file and folder if you want.


 $array_items_folder = array();
 $array_items_file = array();

function directoryToArray($directory, $recursive) {
    $array_items = array();
    if ($handle = opendir($directory)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (is_dir($directory. "/" . $file)) {
                    if($recursive) {
                        $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                    }
                    $file = $directory . "/" . $file;
                    $array_items[] = preg_replace("/\/\//si", "/", $file);
                } else {
                    $file = $directory . "/" . $file;
                    $array_items[] = preg_replace("/\/\//si", "/", $file);
                }
            }
        }
        closedir($handle);
    }
    return $array_items;
}

Open in new window

Avatar of rgb192

ASKER

directoryToArray('C:/Users/Acer/Documents/','TRUE');
directoryToArray('C:/Users/Acer/Documents/',1);

what is second parameter
second parameter is boolean true. If you want to iterate through directory hierachy, else false
directoryToArray('C:/Users/Acer/
Documents/','true');
Sorry, the second parameter should be without qote.

directoryToArray('C:/Users/Acer/Documents/',true);
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Technically you're using the same approach as before, except you use is_file() to determine if something is a file, while you use is_dir() to determine if something's a directory.