Omzy Posted January 11, 2010 Share Posted January 11, 2010 Just having a little trouble creating the code to check if a directory is empty, if not then get the list of files in that directory excluding hidden/system files. Anyone able to advise? Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/ Share on other sites More sharing options...
Omzy Posted January 11, 2010 Author Share Posted January 11, 2010 Anyone able to help? Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992698 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 $contents = glob('/directory/*'); if ($contents) { foreach ($contents as $file) { echo $file . "\n"; } } Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992701 Share on other sites More sharing options...
Omzy Posted January 11, 2010 Author Share Posted January 11, 2010 Cheers thorpe, that's perfect. Just one slight issue in that it returns true if there are hidden files in the folder. How can I exclude hidden files? Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992706 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 It shouldn't. What exactly do you mean by hidden files? Your not using windows are you? Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992707 Share on other sites More sharing options...
Omzy Posted January 11, 2010 Author Share Posted January 11, 2010 lol yes i'm developing this site on apache localhost on windows. i take it'll work just fine on linux server. Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992710 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 Yeah, windows hidden files work differently and aren't recognized as being hidden simply by name. I'm not sure how you would do it on windows to be honest. stat might help you out, though you will need check each file within the loop. Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992711 Share on other sites More sharing options...
Omzy Posted January 11, 2010 Author Share Posted January 11, 2010 OK cool. How about if I limit it to check for certain extensions? The only ones I need to check for are .doc, .docx and .pdf Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992776 Share on other sites More sharing options...
calmchess Posted January 11, 2010 Share Posted January 11, 2010 i have bat file that does what you want all you have to do is call it via exec command yo ntrested? Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-992795 Share on other sites More sharing options...
Omzy Posted January 11, 2010 Author Share Posted January 11, 2010 Bump for thorpe Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-993143 Share on other sites More sharing options...
mikesta707 Posted January 11, 2010 Share Posted January 11, 2010 you can use the GLOB_BRACE flag to specify that you want to use the brace syntax in the glob pattern. you can use that to specify different extensions. for example $glob = glob("director/*.{pdf,doc,docx}", GLOB_BRACE); check out the manual entry for more information Link to comment https://forums.phpfreaks.com/topic/188035-check-if-directory-is-empty-read-files-from-directory/#findComment-993155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.