include is not working

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
kawakes
Forum Newbie
Posts: 5
Joined: Wed Feb 23, 2011 4:21 pm

include is not working

Post by kawakes »

Hey,
First off, please know that I'm a complete noob in php and only have started recently. I'm trying to create a random image generator for my site although, I'm having one big problem. The include for the php script is not working, I have several includes in my site and they all work but this include is refusing to.

Here is the error message I'm getting. Note, I have throughly made sure there are no errors in spelling or anything that could be causing the include to fail.

Warning: include(C:\inetpub\wwwroot/includes/random_img.php) [function.include]: failed to open stream: No such file or directory in C:\inetpub\wwwroot\kawakestudios\index.php on line 1

Here is the code:

<?php include ('./includes/random_img.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml1 ... tional.dtd">
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: include is not working

Post by pickle »

The file you're viewing, index.php, is in a subfolder called "kawakestudios". The file you're including, I'm assuming, sits in kawakestudios/include/random_img.php. As far as I can tell, your include() call should work. Since you're running Windows, I wonder if maybe the './' is causing problems. That's superfluous anyway, so take that off & see what happens.

If that doesn't work, output the result of get_cwd() in your index.php to see what directory PHP thinks its working in.

Finally, I can't imagine why you would include that file at that spot? what does random_img.php do? Does it output html code pointing to a random image? Does it act like an image itself?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kawakes
Forum Newbie
Posts: 5
Joined: Wed Feb 23, 2011 4:21 pm

Re: include is not working

Post by kawakes »

Thanks for the help. The ./ was causing the error but now it's working. The random_img.php is used to select a random image and it's caption by using a multidimensional array. The script is this:
<?php
$images = array(

array('file' => 'copyright',
'caption' => 'The copyright php script'),

array('file' => 'demo',
'caption' => 'Screen shot of Demo 10'),

array('file' => 'ship',
'caption' => 'Very early demo of the engine'),

array('file' => 'imgen',
'caption' => 'The random image generator'),
);

//image counter & Generator
$num_images = count($images);

$max = $num_images - 1;

$i = rand(0, $max); //random from 0 to max

$selected_image = "images/{$images[$i]['file']}.jpg";

$caption = $images[$i]['caption'];

if (file_exists ($selected_image) && is_readable($selected_image)) {
$image_size = getimagesize($selected_image);
}

There are some other parts that are used in the index file to set the image width for the caption.
Thanks again :)
Post Reply