-2

Here is my php code:

function readDirectory($path) {
$handle = opendir($path);
while( $item = readdir($handle) !== false) {

    if($item != "." && $item != "."){
        if (is_file($path . "/" . $item)) {
            $arr ['file'] [] = $item;
        }
        if (is_dir($path . "/" . $item)) {
            $arr ['dir'] [] = $item;
        }

    }
}
closedir($handle);
return $arr;  //line 24

}

$path = "file";
print_r(readDirectory($path));

But constantly getting these errors:

Notice: Undefined variable: arr in /Users/tyrant/workspace/apache/fileManager/dir.func.php on line 24

How do I resolve this problem?

XBroder
  • 187
  • 1
  • 2
  • 8
  • An error has occurred because that variable isn't defined. Where do you think you've first used the variable $arr? – Luke Jun 23 '15 at 06:55
  • Please declare $arr as array under the function: $arr = array(); it will fixed the error. – Iffi Jun 23 '15 at 07:00

2 Answers2

0

just build an empty arr in your function

function readDirectory($path) {
  $handle = opendir($path);
  $arr = array();
  while( $item = readdir($handle) !== false) {

      if($item != "." && $item != "."){
        if (is_file($path . "/" . $item)) {
          $arr ['file'] [] = $item;
      }
      if (is_dir($path . "/" . $item)) {
          $arr ['dir'] [] = $item;
     }

  }
 }
 closedir($handle);
 return $arr;  //line 24
}

$path = "file";
print_r(readDirectory($path));

so it will return allways an array and a filled array if your logic fires

donald123
  • 5,638
  • 3
  • 26
  • 23
0

You need to create $arr instance, just like that: $arr = array();

slummer87
  • 42
  • 4