Ok, I’ve written a small piece of PHP to recursively go through a directory with images and display every image, in random order, with a javascript fade in and fade out effect. Every time it displays an image it writes the filename to a TXT file so it knows what it’s already displayed, then refreshes to display the next image. If it’s displayed all the images, it clears the TXT file and starts over again. However, it’s still showing the same file more than once in a set. Is there something I’m doing wrong when checking if the image has already been displayed? Here’s the code:
[code=php]<?php
//recursively get image files from root and all directories within
getfiles(“.”);
function getfiles($folder) {
global $files;
if ($handle=opendir(“$folder”)) {
while(false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
if (is_dir($file)) {
$path=$folder.”/”.$file;
getfiles($path);
}
elseif(substr($file,-3)==’gif’ || substr($file,-3)==’jpg’ || substr($file,-3)==’bmp’ || substr($file,-3)==’png’) {
$files[count($files)] = $folder.”/”.$file;
}
}
}
}
closedir($handle);
}
//pick a random file
$random=rand(0,count($files)-1);
$myFile = “display.txt”;
//check if file has already been displayed
if (file_exists($myFile)) {
$contents=file($myFile);
if (count($files)<=count($contents))
//displayed all files, clear the log
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
else {
$contents=file_get_contents($myFile);
while (strpos($contents,$files[$random])!=false) {
//already displayed file, pick a new one
$random=rand(0,count($files)-1);
}
//open log for writing
$fh = fopen($myFile, ‘a’) or die(“can’t append file”);
}
}
else $fh = fopen($myFile, ‘w’) or die(“can’t open file”);
$stringData = $files[$random].”n”;
//write file to log
fwrite($fh, $stringData);
fclose($fh);
echo <<<HTML
<html>
<head>
<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin
var ie5=(document.getElementById && document.all);
var ns6=(document.getElementById && !document.all);
nPlus = 5; //the % of fading for each step
speed = 50; //the speed
nOpac = 0;
function FadeIn(){
if(document.getElementById){
document.getElementById(‘img1’).style.visibility=”visible”;
imgs = document.getElementById(‘img1’);
opacity = nOpac+nPlus;
nOpac = opacity;
setTimeout(‘FadeIn()’,speed);
if(opacity>350 || opacity<0){
FadeOut();
}
if(ie5){
imgs.style.filter=”alpha(opacity=0)”;
imgs.filters.alpha.opacity = opacity;
}
if(ns6){
imgs.style.MozOpacity = 0 + ‘%’;
imgs.style.MozOpacity = opacity + ‘%’;
}
}
}
nOpac2 = 100;
function FadeOut(){
if(document.getElementById){
document.getElementById(‘img1’).style.visibility=”visible”;
imgs = document.getElementById(‘img1’);
opacity = nOpac2-nPlus;
nOpac2 = opacity;
setTimeout(‘FadeOut()’,’10’);
if(opacity>100 || opacity<0){
history.go(0);
}
if(ie5){
imgs.style.filter=”alpha(opacity=0)”;
imgs.filters.alpha.opacity = opacity;
}
if(ns6){
imgs.style.MozOpacity = 0 + ‘%’;
imgs.style.MozOpacity = opacity + ‘%’;
}
}
}
onload=FadeIn;
// End –>
</script>
<title>Slideshow</title>
</head>
<body style=”background-color: #000000;”>
<div id=”img1″ style=”position:absolute;top:$top;left:$left;z-index:2;visibility:hidden;}”>
<img id=”picture” src=”./imagetest.php?file=$files[$random]&width=1024&height=768″>
</div>
</body>
</html>
HTML;
?>