Solved

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

Posted on 2013-01-15
8
207 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:Rahul Gupta
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:Rahul Gupta
ID: 38781470
second parameter is boolean true. If you want to iterate through directory hierachy, else false
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Expert Comment

by:Rahul Gupta
ID: 38781481
directoryToArray('C:/Users/Acer/
Documents/','true');
0
 
LVL 4

Expert Comment

by:Rahul Gupta
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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