Need Help with PHP code

Fair warning I’m very new at PHP coding

I’m trying to to write PHP code to display random pictures from a directory one at a time. I’ve found one to display all the pictures at once, but not just one at a time

?php
$dir = ‘Pictures/’;
opendir($dir);

//open dir
if ($opendir = opendir($dir))

{

//read dir
while (($file = readdir($opendir)) !==FALSE)
{
	if($file!="."&&$file!="..")
		echo "<img src='$dir/$file'style='width:1750px;'><br>";
}

}

The correct way to randomize a list is…
http://php.net/manual/en/function.shuffle.php

Once randomized, just use the first entry in the list. (You could also directly pick an entry at random but that will require a bit more thought and code.) (Oops. Looks like PHP has just what you need… http://php.net/manual/en/function.array-rand.php)

Your next mission is to fill a list (array) with filenames instead of outputting img tags. Then apply what you learned earlier in this post.

Stack overflow was probably already on your list of stops…

2 Likes