Chappers Posted July 9, 2011 Share Posted July 9, 2011 Hi, I've read of is_dir problems in older versions of php, so checked and server is using 5.2.13. This simple bit of code: <?php $contents = scandir('/home/*******/public_html/images/'); foreach ($contents as $content) { if (is_dir ($content)) { echo "$content is a directory<br>"; } else { echo "$content is NOT a directory<br>"; }} ?> outputs: . is a directory .. is a directory folder1 is NOT a directory folder2 is NOT a directory folderthree is NOT a directory Those are the three folders I created for test purposes. Is_file works as anticipated, and I know I can use it and other workarounds. But why wouldn't is_dir be working? Have I done something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/ Share on other sites More sharing options...
trq Posted July 9, 2011 Share Posted July 9, 2011 How did you create these directories exactly? Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240348 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2011 Share Posted July 9, 2011 LOL, because each folder has a . and .. entry, you will get a TRUE from the is_dir(), no matter where your script is executed at. The only way your script would give you TRUE values from is_dir() for specific folders within the images folder, is if the script is executed from inside the images folder. You would need to build either a relative or absolute path in the if(){} statement - if (is_dir ($path/$content)) { Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240357 Share on other sites More sharing options...
Chappers Posted July 9, 2011 Author Share Posted July 9, 2011 How did you create these directories exactly? Using flashfxp, I FTP'ed to the website and right-clicked and chose make folder. @PFMaBiSmAd: Two problems with your post; the LOL which is patronising, but since you obviously know so much, I'm sure it's acceptable to take the p*ss out of those who don't. Second problem; if you read my post including the code output, you'd see is_dir ISN'T returning TRUE for everything it finds like you claim it must do. Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240463 Share on other sites More sharing options...
wildteen88 Posted July 9, 2011 Share Posted July 9, 2011 You need to give is_dir the full path to the folder. You cant just give is_dir the name of the folder. Change your code to $search_path = '/home/*******/public_html/images/'; $contents = scandir($search_path); foreach ($contents as $content) { if (is_dir ("$search_path/$content")) { echo "$search_path/$content is a directory<br>"; } else { echo "$search_path/$content is NOT a directory<br>"; }} Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240466 Share on other sites More sharing options...
Chappers Posted July 9, 2011 Author Share Posted July 9, 2011 Thanks wildteen88, works perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240470 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2011 Share Posted July 9, 2011 Here's the first statement written out with all the qualifications and conditions, since it was apparently misunderstood - because each folder (except the disk root) has a . and .. entry, you will get a TRUE from the is_dir() (for the . and .. entry, what this sentence is referring to), no matter where your script is executed at. Quote Link to comment https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/#findComment-1240571 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.