Solved

store in a directory/files array

Posted on 2013-01-17
2
249 Views
Last Modified: 2013-01-17
this question is related to
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27998415.html

function readDirs($path){
  $dirHandle = opendir($path);
  while($item = readdir($dirHandle)) {
    $newPath = $path."/".$item;
    if(is_dir($newPath) && $item != '.' && $item != '..') {
       echo "Found Folder $newPath<br>";
       readDirs($newPath);
    }
    else{
      echo '&nbsp;&nbsp;Found File or .-dir '.$item.'<br>';
    }
  }
}

$path =  "drupal";
echo "$path<br>";

readDirs($path);

Open in new window




is there a way to put this in an array
so I can differ between
'f'ile
and
'd'ir

and I do a
foreach($array as $key=>$filename){
0
Comment
Question by:rgb192
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38788405
Try this:

<?php
error_reporting(E_ALL);

$myArray = array();

function readDirs($path){
  $dirHandle = opendir($path);
  while($item = readdir($dirHandle)) {
    $newPath = $path."/".$item;
    if(is_dir($newPath) && $item != '.' && $item != '..') {
       echo "Found Folder $newPath<br>";
        $myArray['dirs'][] = $newPath;
       readDirs($newPath);
    }
    else{
      echo '&nbsp;&nbsp;Found File or .-dir '.$item.'<br>';
        $myArray['files'][] = $newPath;
    }
  }
  return $myArray;
}

$path =  ".";
echo "$path<br>";

$myArray = readDirs($path);

echo '<pre>';
print_r($myArray);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38790183
thanks

I want there to be a way to tell which dir the file is in similar to a tree

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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.

773 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