/    Sign up×
Community /Pin to ProfileBookmark

Trouble counting images in a directory

Hey guys,
I apologize in advance if this is a simple question, but I’m having trouble counting the number of images in a directory. I’m working in WordPress and I’m just looking to load a random image out of a folder directory based on what page you’re on. If the page you’re one doesn’t have a corresponding folder of images, it will just look in a default folder. Here’s what I’ve got:

[code=php]function random_background($id) {
$slugdirectory = new_slug($id); //a function that pulls the page’s slug
$dir = bloginfo(‘stylesheet_directory’);
$directory = $dir.’/images/page-backgrounds/’.$slugdirectory.’/’;

if (glob(“$directory*.jpg”) == false) {
$directory = $dir.’/images/page-backgrounds/default/’;
}

$count = 0;
$filenames = array();
$filepath = $directory . “*”;
foreach (glob($filepath) as $filename) {
array_push($filenames , $filename);
$count++;
}
$rand = rand(0, ($count-1));

return $direcotry.”.$filenames[$rand];
}[/code]

For one reason or another, my files don’t see to be getting counted and the array $filenames just get returns blank.

Does anybody have any bright ideas? Thanks in advance.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jacobdeschauthorJul 21.2011 — Ok, it turns out that the problem is happening when I'm trying to combine bloginfo('stylesheet_directory') with my custom path to the images. It's definitely outputting the stylesheet directory string, but it doesn't seem to be combining it with the /images/page-backgrounds... string. What am I doing wrong here?
Copy linkTweet thisAlerts:
@jacobdeschauthorJul 21.2011 — So...it appears that I've got part of the problem fixed. It turns out the built-in Wordpress function bloginfo('stylesheet_directory') already returns a string adding onto it is pretty much a no-no. The one to us is this:
[code=php]get_stylesheet_directory_uri()[/code]

HOWEVER, that still doesn't work for me. The images in the folder still don't get counted... I stripped out everything else and just made a simple test to see if it would count:
[code=php]$directory = get_stylesheet_directory_uri().'/images/page-backgrounds/default/';

$count = count(glob($directory . "*"));

echo $count;
echo $directory;[/code]


Still...nothing... $directory outputs just fine, but I get 0 for $count every time.

But hard-coding
[code=php]$directory = 'wp-content/themes/theme-name/images/page-backgrounds/default/';[/code]

(which I really don't want to do, btw) works just fine
Copy linkTweet thisAlerts:
@jacobdeschauthorJul 21.2011 — Alright, it's working now. It's a total hack job, but it's the only thing I could get to work and I'm tired, so it's happening...

[code=php]function new_random($id) {
$slugdirectory = new_slug($id);
$directory = get_stylesheet_directory().'/images/page-backgrounds/'.$slugdirectory.'/';

if (glob("$directory*.jpg") == false) {
$directory = get_stylesheet_directory().'/images/page-backgrounds/default/';
}

$count = 0;

$filenames = array();
$filepath = $directory . "*";
foreach (glob($filepath) as $filename) {
array_push($filenames , $filename);
$count++;
}
$rand = rand(0, ($count-1));
$dir = $filenames[$rand];
$pos = strrpos($dir, "images");
$image = substr($dir, $pos);

$newimage = get_stylesheet_directory_uri().'/'.$image;

return $newimage;
}[/code]
×

Success!

Help @jacobdesch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...