Jump to content

is_dir() says variable is undefined?


limitbreaker

Recommended Posts

Hi,

So, the following:

require 'panel.php';
$guy = $_GET['user'];
$path = "users/$guy";

$sql = mysql_query("SELECT * FROM users WHERE id='$guy'");
$row = mysql_fetch_array($sql);

$folders = array_filter(scandir($path), function($item) { 
     return is_dir($path . $item); });

echo "<title>WeLeague - ${row['display']}'s Media</title>"; 

returns only the folders in a directory (mysql is already connected in panel.php). The problem is, when I echo $folders in a for loop, I get the following error 4 times:

 

"Notice: Undefined variable: path in C:\Program Files (x86)\EasyPHP-12.1\www\www.weleague.org\media.php on line 12" [line 12 being the is_dir line]

 

Then it proceeds to echo . and .. (the current and parent directories). Maybe I'm missing something?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/
Share on other sites

  On 8/1/2013 at 4:59 AM, PravinS said:

 

try by defining $path variable as global in function() or try replacing this line of code

$folders = array_filter(scandir($path), function($item) {
 global $path;
     return is_dir($path . $item); });

Worked like a charm, thank you!

 

  On 8/1/2013 at 5:43 AM, Psycho said:

 

Don't use GLOBAL - it's bad form. Your function is only returning the result of the isdir() command - so why wrap it in a function?

$folders = array_filter(scandir($path), is_dir($path . $item));

Thanks for the help, but with this I can't seem to get any children to $item

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.