jcpeden Posted June 30, 2011 Share Posted June 30, 2011 I'm trying to count the number of files in a directory on my local server. When I browse to the directory I can see this: Here is the code that I'm using: $category = get_the_category(); $cat_slug = $category[0]->category_nicename; $dir = 'http://localhost/badsexsucks/wp-content/themes/AskIt/images/gay/'; if (glob("$dir*.jpg") != false) { $filecount = count(glob("" .$dir. "*.jpg")); echo $filecount; } else { echo 0; } For some reason, it always returns 0 I've tried both: $dir = 'http://localhost/badsexsucks/wp-content/themes/AskIt/images/gay/'; $dir = '../images/gay/' Quote Link to comment https://forums.phpfreaks.com/topic/240788-count-files-in-a-directory-always-returns-0/ Share on other sites More sharing options...
salathe Posted June 30, 2011 Share Posted June 30, 2011 You can't use a web address with glob like that. Specify the folder path on the filesystem instead (e.g. C:\blah\something\images\gay). Quote Link to comment https://forums.phpfreaks.com/topic/240788-count-files-in-a-directory-always-returns-0/#findComment-1236760 Share on other sites More sharing options...
jcpeden Posted July 1, 2011 Author Share Posted July 1, 2011 I've tried both: $dir = 'http://localhost/badsexsucks/wp-content/themes/AskIt/images/gay/'; $dir = '../images/gay/' So, if I can't use a web address with glob, should the relative link work? Here is an image to show you the file structure. The file running this function is entry.php and is located in includes. Quote Link to comment https://forums.phpfreaks.com/topic/240788-count-files-in-a-directory-always-returns-0/#findComment-1237185 Share on other sites More sharing options...
salathe Posted July 1, 2011 Share Posted July 1, 2011 Yes, the relative path will work. Remember that relative paths aren't necessarily relative to the file that the code is in, but instead the "current working directory". Quote Link to comment https://forums.phpfreaks.com/topic/240788-count-files-in-a-directory-always-returns-0/#findComment-1237197 Share on other sites More sharing options...
jcpeden Posted July 1, 2011 Author Share Posted July 1, 2011 Thanks I didn't know that! I was able to get the code working by figuring out the working directory using getcwd: http://php.net/manual/en/function.getcwd.php Quote Link to comment https://forums.phpfreaks.com/topic/240788-count-files-in-a-directory-always-returns-0/#findComment-1237276 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.