Jump to content

is_dir() says variable is undefined?


Go to solution Solved by PravinS,

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!

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

 

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!

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.