limitbreaker Posted August 1, 2013 Share Posted August 1, 2013 (edited) 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 August 1, 2013 by limitbreaker Quote Link to comment https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/ Share on other sites More sharing options...
Solution PravinS Posted August 1, 2013 Solution Share Posted August 1, 2013 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); }); Quote Link to comment https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/#findComment-1442888 Share on other sites More sharing options...
Psycho Posted August 1, 2013 Share Posted August 1, 2013 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)); Quote Link to comment https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/#findComment-1442891 Share on other sites More sharing options...
limitbreaker Posted August 1, 2013 Author Share Posted August 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/#findComment-1442904 Share on other sites More sharing options...
doddsey_65 Posted August 1, 2013 Share Posted August 1, 2013 A better way to avoid global would be to specify to use $path in the closure $folders = array_filter(scandir($path), function($item) use ($path) { return is_dir($path . $item); }); Quote Link to comment https://forums.phpfreaks.com/topic/280696-is_dir-says-variable-is-undefined/#findComment-1442946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.