Valhalla Legends Forums Archive | Web Development | PHP Annoyance

AuthorMessageTime
Disco
What I'm trying to do is make one of those pages where everytime you hit refresh, a different image randomly loads.  My friend gave me this exact script (the only difference being the urls and image directories), wich he used and it worked fine for him.  The problem is for me it always ends up just displaying that little square with the tear through it showing the image isn't found.  Here's what the script(s) look like.

This is the html that loads on the initial page:

[code]<div style="position:absolute; top: 5px; right: 10px; z-index:1;"><a href="http://www.dougisawesome.com/Stuff/images/rand" target="_blank"><img src="http://www.dougisawesome.com/Stuff/images/sig.php" border="0"></a></div>[/code]

Which, in turn, loads this php script that's SUPPOSED to be displaying a random image from the directory.

[php]<?php
$wtf = "Stuff/images/rand";
srand( time() );
if ($dir = @opendir($wtf)) {
while (($file = readdir($dir)) !== false) {
if ( eregi( '.gif$', $file ) ) { $files[] = $file; }
}
closedir($dir);
}
$file = $files[rand() % sizeof( $files )];
header( "Content-Type: image/gif" );
header( "Content-Length: " . filesize( $wtf."/".$file ) );
readfile( $wtf."/".$file );
?>[/php]

Since I'm about as good at web coding as I am at flying an F18 jet, try to bare with me when I ask questions like these that are probably very easily solved.
January 29, 2006, 2:09 AM
Kp
The script looks ok.  Check that the files in <webroot>/Stuff/images/Stuff/images/rand/ have the right file extension and are readable by the script.  Also, fix your script and use a more modern image format like portable network graphics.
January 29, 2006, 2:28 AM
Disco
Well I did what you said, I changed all the images to .PNG and edited the script to load those instead of .gif.  There error I read when I do "View Page Source" under http://www.dougisawesome.com/Stuff/images/sig.php is:

[code]<br />
<b>Warning</b>:  filesize(): Stat failed for Stuff/images/rand/ (errno=2 - No such file or directory) in <b>/home/.carroll/dougisawesome/dougisawesome.com/Stuff/images/sig.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/.carroll/dougisawesome/dougisawesome.com/Stuff/images/sig.php:12) in <b>/home/.carroll/dougisawesome/dougisawesome.com/Stuff/images/sig.php</b> on line <b>12</b><br />
<br />

<b>Warning</b>:  readfile(Stuff/images/rand/): failed to open stream: No such file or directory in <b>/home/.carroll/dougisawesome/dougisawesome.com/Stuff/images/sig.php</b> on line <b>13</b><br />
[/code]

After seeing this I thought changing the directory in the php script from just "Stuff/images/rand" to "home/.carroll/dougisawesome/dougisawesome.com/Stuff/images/rand" hoping that might make a difference, but I still recieved the same error as above so I changed it back to its original form.

Would there be any way to accomplish what I'm trying to do without the php and using just html?
January 29, 2006, 3:26 AM
rabbit
No.
January 29, 2006, 3:44 PM
Kp
Apparently subtle hints aren't enough.  Your script is being executed with . equal to the directory in which the script resides.  That's why I told you to check <webroot>/Stuff/images/Stuff/images/rand -- because that's where it's looking for the files to send.  I'd hoped you'd guess that from the error output of your script.
January 29, 2006, 6:01 PM
Arta
Don't use eregi there, either. "." is a wildcard character (it'll match anything). Use if(substr($file, -4) == '.gif').
January 30, 2006, 6:00 PM
rabbit
For extension checking, I use the following:
[code] function getExtension($page)
{
$dot = strrpos(basename($page), '.');

if($dot)
{
$bits = explode('.', $page);
$ext = $bits[count($bits) - 1];
return $ext;
} else
return '';
}[/code]

That get's the extension, and then I use inarray() to check it vs a premade array of allowed extensions.
January 30, 2006, 9:50 PM
JTN Designer
[php]
<?php
//Make sure the pictures are in the same directory, for this examples sake.
$array = array("image.jpg", image.gif", "image.png", image.bmp");
$rand  = array_rand($array);

echo $rand;
?>
[/php]

It's as simple as that.
February 17, 2006, 6:06 AM

Search