I’m trying to display two randomly selected images from a dir
Instead of images, img.names are displayed
$dir = 'memb_area/captcha/imgs/';
$files = scandir($dir);
$rand_keys = array_rand($files, 2);
echo $files[$rand_keys[0]] . "\n";
echo $files[$rand_keys[1]] . "\n";
Also tried:
echo '<img src="memb_area/captcha/imgs">' + 'echo $files[$rand_keys[0]] . "\n";'
And – is it possible to print these pictures inside a separate div on a page?
+is not a valid PHP concatenation character..is. By using+, you are effectively adding these two strings, which when converted to integers, equate to0.This line:
echo '<img src="memb_area/captcha/imgs">' + 'echo $files[$rand_keys[0]] . "\n";'should be:(updated)
echo "<img src=\"memb_area/captcha/imgs/".$files[$rand_keys[0]]."\">".PHP_EOL;Update 01: (OP’s Comment: but after refreshing, after two or three times – nothing is displayed. Next refresh – image is displayed… and so on.)
Scandir php.net (Example #1) says that:
So, maybe your
scandir($foo)is returning an array with two keys having invisible directories as their values.Try this code and let me know: