Valhalla Legends Forums Archive | Web Development | PHP Annoyance

DiscoWhat 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:

<div style="position:absolute; top: 5px; right: 10px; z-index:1;"><a href="" target="_blank"><img src="" border="0"></a></div>

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

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

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 28, 2006, 08:09 PM
KpThe 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 28, 2006, 08:28 PM
DiscoWell 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 is:

<br />
<b>Warning</b>:  filesize(): Stat failed for Stuff/images/rand/ (errno=2 - No such file or directory) in <b>/home/.carroll/dougisawesome/</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/ in <b>/home/.carroll/dougisawesome/</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/</b> on line <b>13</b><br />

After seeing this I thought changing the directory in the php script from just "Stuff/images/rand" to "home/.carroll/dougisawesome/" 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 28, 2006, 09:26 PM
rabbitNo.January 29, 2006, 09:44 AM
KpApparently 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, 12:01 PM
Arta[vL]Don't use eregi there, either. "." is a wildcard character (it'll match anything). Use if(substr($file, -4) == '.gif').January 30, 2006, 12:00 PM
rabbitFor extension checking, I use the following:
function getExtension($page)
$dot = strrpos(basename($page), '.');

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

That get's the extension, and then I use inarray() to check it vs a premade array of allowed extensions.
January 30, 2006, 03:50 PM
JTN Designer<?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);


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