Solved

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

Posted on 2013-01-15
8
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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 35

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 35

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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 …

617 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