/    Sign up×
Community /Pin to ProfileBookmark

What am I doing wrong?

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;
?>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@FiZiXauthorOct 19.2006 — Can anybody help?
Copy linkTweet thisAlerts:
@scousesheriffOct 19.2006 — Just a tip:

When using strpos, it is possible to find the proverbial needle in the haystack and still have strpos (haystack, needle) != false evaluate to true. This is because 0 evaluations to false. I would recommend changing your code to:

[code=php](strpos($contents,$files[$random])!==false) [/code]

Can I ask how imagetest.php works? And why dont you just have the following for the image src:
[code=php]src="$files[$random]"[/code]
Copy linkTweet thisAlerts:
@FiZiXauthorOct 20.2006 — Cool, that seems to have done it. What's the difference between "!=" and "!==" in PHP?

Can I ask how imagetest.php works? And why dont you just have the following for the image src:

PHP Code:

src="$files[$random]" [/quote]


imagetest.php just resizes the image to the value specified. I used to do it in the browser but that doesn't look as nice.
Copy linkTweet thisAlerts:
@scousesheriffOct 20.2006 — == Tests if the two items are equal

=== Tests if the two items are equal and the same type (that is the items are identical)

and so it is for != (not equal) and !== (not identical)

so

0 == false

is true

but

0 === false

is false as they are different types.

Make sense?
Copy linkTweet thisAlerts:
@FiZiXauthorOct 20.2006 — Yup! Thanks for the explanation.
Copy linkTweet thisAlerts:
@frank_kelly_101Oct 20.2006 — Is it better to use $_request instead of $_POST or $_GET? Since it does what $_POST and $_GET do anyway. Is there a drawback to using it?
Copy linkTweet thisAlerts:
@scousesheriffOct 20.2006 — Is it better to use $_request instead of $_POST or $_GET? Since it does what $_POST and $_GET do anyway. Is there a drawback to using it?[/QUOTE]
This probably should have been in another thread, but...

$_REQUEST will also look in $_COOKIE aswell as $_GET and $_POST, so it is best to be specific if you can as you could have different values in $_COOKIE and $_Get/$_POST which could lead to confusion to the user.
×

Success!

Help @FiZiX 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...