• Welcome to Valhalla Legends Archive.
 

PHP Annoyance

Started by disco, January 28, 2006, 08:09 PM

Previous topic - Next topic

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:

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

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

<?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 );?>


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.
Say it with me:


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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

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:

<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 />


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?
Say it with me:


rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Arta

Don't use eregi there, either. "." is a wildcard character (it'll match anything). Use if(substr($file, -4) == '.gif').

rabbit

For extension checking, I use the following:
function getExtension($page)
{
$dot = strrpos(basename($page), '.');

if($dot)
{
$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.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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);echo $rand;?>?>


It's as simple as that.
(advertising deleted by forum administrator)