Jump to content

[SOLVED] Random Image w/ PHP5


Shadow Wolf

Recommended Posts

My server host just upgraded to PHP5 and mySQL5. Unfortunately this means it broke a few scripts that were working so I need a new method to get the same effect.

 

I was using rid.php (random image script) that was fairly simple. I drop it into a directory with images, then link to it and it will pull the images up in the directory. There was never any need for me to update the script when I added images, I just needed to add the images to the directory.

 

<?php

//read folder
$folder=opendir("."); 
while ($file = readdir($folder)) 
$names[count($names)] = $file; 
closedir($folder);

//sort file names in array
sort($names);

//remove any non-images from array
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}

//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);

//random image from array
$slika=$names1[$rand_keys[0]]; 

//image dimensions
$dimensions = GetImageSize($slika); 
if (isset($pic)){header ("Location: $slika");}
else {echo "<img src=\"$slika\" $dimensions[3]>";}
?>

 

I would then use the following code in my html to call upon it and it worked fine with years... except now it will not work anymore:

<?
function view_logo() {
global $mklib;
return <<<EOF

<!-- begin logostrip -->
  
<tr>
  <td id="mklogostrip" height="100" width="100%">
  
<a href="$mklib->siteurl/index.php"><img src="http://www.protoculturex.net/images/header/rid.php?pic=random" border="0" alt="PCX" /></a>

          </td>
</tr>

<!-- end logostrip -->

EOF;
}
?>

 

I used the script in a <img tag because it basically tricked it into thinking it was an image and didn't require me to make any modifications to the script trying to mess with an echo or get it to display the image with the way the .php is setup in the Portal software.

 

 

I tried to use a random .php script and replace the image tag using the following below. But for some reason it isn't working. It displays all the images, not a random one and it seems the closing tags for the .php part isn't working right. It treats it like regular HTML.

 

<?
function view_logo() {
global $mklib;
return <<<EOF

<!-- begin logostrip -->
  
<tr>
  <td id="mklogostrip" height="100" width="100%">

<?
$bannerAd[1] = '<a href="http://www.protoculturex.net/" target="_blank"><img src="/images/header/bleach.jpg" width="903" height="259" border="0"></a>';
$bannerAd[2] = '<a href="http://www.protoculturex.net/" target="_blank"><img src="/images/header/bucktick2.jpg" width="903" height="259" border="0"></a>';

$adCount = count($bannerAd);
$randomAdNumber = mt_rand(1, $adCount);
echo $bannerAd[$randomAdNumber];
?>

    </td>
</tr>

<!-- end logostrip -->

EOF;
}
?>

 

 

So is there an easier way to do this or a method to update the way I was doing it earlier? Any help and suggestions would be appreciated, thank you. Keep in mind I'm pretty much a newbie when it comes to php/mysql, this just seemed like the easiest way to do it at the time.

 

 

Link to comment
https://forums.phpfreaks.com/topic/68883-solved-random-image-w-php5/
Share on other sites

Solved the issue.

 

With the server migration they had some security settings, even though they said it had nothing to do with the upgrade. The file was considered writeable with 644 permissions so I had to set it to 444 permissions then everything worked fine.

 

Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.