Solved

store in a directory/files array

Posted on 2013-01-17
2
253 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP substring 3 52
Change background images after 5 seconds. 12 46
How to convert PHP array into string ? 16 24
Sending JSON Via POST In PHP 7 26
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 dynamically set the form action using jQuery.
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 …

734 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