Solved

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

Posted on 2013-01-15
8
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 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

9 Experts available now in Live!

Get 1:1 Help Now