Solved

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

Posted on 2013-01-15
8
205 Views
Last Modified: 2013-01-16
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/
0
Comment
Question by:rgb192
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:rahulgupta9x
ID: 38781325
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

0
 

Author Comment

by:rgb192
ID: 38781370
directoryToArray('C:/Users/Acer/Documents/','TRUE');
directoryToArray('C:/Users/Acer/Documents/',1);

what is second parameter
0
 
LVL 4

Expert Comment

by:rahulgupta9x
ID: 38781470
second parameter is boolean true. If you want to iterate through directory hierachy, else false
0
 
LVL 4

Expert Comment

by:rahulgupta9x
ID: 38781481
directoryToArray('C:/Users/Acer/
Documents/','true');
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:rahulgupta9x
ID: 38781486
Sorry, the second parameter should be without qote.

directoryToArray('C:/Users/Acer/Documents/',true);
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38782247
Just change:

        if ($entry != "." && $entry != ".." && is_file($folder_location.''.$entry)) {
            echo "$entry<br />";
            $filesarray[] = $entry;
        }

To:

        if ($entry != "." && $entry != "..")
        {
            if(is_file($folder_location.''.$entry)) {
                echo "$entry<br />";
                $filesarray[] = $entry;
            }
            elseif(is_dir($folder_location.''.$entry)) {
                echo "$entry<br />";
                $dirsarray[] = $entry;
            }
        }

And this:

$filesarray = array();

To:

$filesarray = array();
$dirsarray = array();
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38782250
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.
0
 

Author Closing Comment

by:rgb192
ID: 38785584
thanks

I have a related question to read all the files in the folder

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27998415.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now