azharnusayef
asked on
displaying all images randomly from a folder
hi i have the following code that displays images randomly from a given folder.
but this code only displays one image at a time. i want to display all the images in the folder so if i have 10 images
i want them to be displayed in a table how can i do that
for example if i have
1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg 7.jpg 8.jpg 9.jpg 10.jpg
this is my code that displays one random image
but this code only displays one image at a time. i want to display all the images in the folder so if i have 10 images
i want them to be displayed in a table how can i do that
for example if i have
1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg 7.jpg 8.jpg 9.jpg 10.jpg
this is my code that displays one random image
<?php
$IMAGE_DIRECTORY = "index_files/";
###Nothing below this line should need to be configured.###
$line = "";
$randomNumber= 0;
$imageFiles = array();
$fileName = "";
$x=0;
if ($DIR = opendir($IMAGE_DIRECTORY)) {
while ($fileName = readdir($DIR)) {
if (is_dir($IMAGE_DIRECTORY . $fileName)) { continue; }
if (!preg_match("/\w/", $fileName)) { continue; }
if (preg_match("/\.gif$|\.jpg$|\.jpeg$/i",$fileName)) {
$imageFiles[$x] = $fileName;
$x+=1;
}
}
closedir($DIR);
srand((double)microtime()*1000000);
$randomNumber = rand(0,(count($imageFiles) - 1));
Header ("Pragma: no-cache\n");
if (preg_match("/\.gif$/i",$imageFiles[$randomNumber])) {
Header ("Content-type: image/gif\n\n");
} elseif (preg_match("/\.jpg$|\.jpeg$/i",$imageFiles[$randomNumber])) {
Header ("Content-type: image/jpg\n\n");
} else { #Not a gif or jpeg file. Exit the program.
exit;
}
$fp = fopen ($IMAGE_DIRECTORY . $imageFiles[$randomNumber], "rb");
echo (fread($fp,filesize($IMAGE_DIRECTORY . $imageFiles[$randomNumber])));
fclose($fp);
}
exit;
# random_image.php
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would recommend opening a new question. This can be done many ways and you might be able to get some good AJAX experts to assist in an elegant solution!
ASKER
but one problem i want that the images to be displayed randomly each time as now they are displayed in the same location each time i refresh the page
After Line 24 in my example ( if( count($imageFiles) > 0 ) { ) add a new line with this information:
shuffle($imageFiles);
Make sure that this is before line 25:
foreach($imageFiles as $field => $value) {
shuffle($imageFiles);
Make sure that this is before line 25:
foreach($imageFiles as $field => $value) {
ASKER
ok, thank you that is working but now my question is how to display check boxes for each image so that
i can save the selected image name into the database??? should i open another question?
i can save the selected image name into the database??? should i open another question?
I would recommend opening a new question. You will get better responses since this is a new topic.
ASKER
thank youuuuuuuuuuuuuuuuuuuuuu
ASKER
selected image names such as (1,2,3,4,...) in my database