Need help with small random image PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The End
Forum Newbie
Posts: 2
Joined: Thu Aug 25, 2011 12:54 pm

Need help with small random image PHP script

Post by The End »

Hello everyone, this is my first post so I'll try to be as classy as possible!

I'm learning PHP programming and would like some help completing this very small project.

What I have is an index.php which includes another php file that displays a random image from a specific folder. Here is the actual code.

barbie.php (main index.php file)

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Title>PHP Demo</Title>
</head>
<body>


					<?php include($_SERVER['DOCUMENT_ROOT']."images/rotate155.php"); ?>




</body>
</html> 
and here is the rotating script

rotate155.php

Code: Select all

<?
 $imglist='';
  //$img_folder is the variable that holds the path to the banner images. Mine is images/tutorials/
// see that you don't forget about the "/" at the end 
 $img_folder = "/155/";

  mt_srand((double)microtime()*1000);

  //use the directory class
 $imgs = dir($img_folder);

  //read all files from the  directory, checks if are images and ads them to a list (see below how to display flash banners)
 while ($file = $imgs->read()) {
   if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
     $imglist .= "$file ";

 } closedir($imgs->handle);

  //put all images into an array
 $imglist = explode(" ", $imglist);
 $no = sizeof($imglist)-2;

 //generate a random number between 0 and the number of images
 $random = mt_rand(0, $no);
 $image = $imglist[$random];

//display image
 echo '<img src="'.$img_folder.$image.'" border=0>';
 ?>

but I get this error

Code: Select all

Warning: include(/home/evilpspc/public_html/barbie.theendstudios.comimages/rotate155.php) [function.include]: failed to open stream: No such file or directory in /home/evilpspc/public_html/barbie.theendstudios.com/barbie.php on line 10

Warning: include() [function.include]: Failed opening '/home/evilpspc/public_html/barbie.theendstudios.comimages/rotate155.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/evilpspc/public_html/barbie.theendstudios.com/barbie.php on line 10

I've been researching how to use includes, requires php.ini, path_include I just can't find out how to make this work though, any help please?

Thank you very much for your time and effort!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Need help with small random image PHP script

Post by social_experiment »

Code: Select all

<?php 
 // try adding a forward slash between images and the document root.
include($_SERVER['DOCUMENT_ROOT']."/images/rotate155.php"); ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
The End
Forum Newbie
Posts: 2
Joined: Thu Aug 25, 2011 12:54 pm

Re: Need help with small random image PHP script

Post by The End »

It looks like I didn't include a slash somewhere but can't figure out where
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: Need help with small random image PHP script

Post by greip »

You can probably solve this by adding a slash between $img_folder and $image in the below line of your code:

Code: Select all

//display image
 echo '<img src="'.$img_folder.$image.'" border=0>';
Post Reply