Random Image Script Not Working With Virutal Paths

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
phpadam
Forum Newbie
Posts: 5
Joined: Wed Dec 10, 2003 9:58 am

Random Image Script Not Working With Virutal Paths

Post by phpadam »

Hello,

I have php-included a pre-written script that is included on every page on my site. The script goes into a folder and randomly selects an image from the folder to display. It works perfectly when the file on which the script is included resides in my root directory (on an index.php page). However, as soon as I copy the exact same page into a subdirectory, the script no longer works. I am going to include the full PHP script, followed by the HTML code that calls the script. Thank you to anyone and everyone that takes the time to help me out!

Helpful notes:

1) It works on a file in the root directory, index.php
2) It fails on a file in a subdirectory, such as pages/2/music/cdcollection.php
3) On both pages, the script is supposed to be picking an image from the same folder, which from the root directory is at images/photobar
4) On the index.php, I reference the images folder as images/photobar, but on the HTML I include below, I reference the images folder as /images/photobar, since my file is in a subdirectory and needs to go back to the root first.

THE PHP CODE

<?php
$this_folder=getcwd();
function random_image($browse_subdirs="",$tcn_img_folder=""){
global $this_folder;
if($tcn_img_folder!=""){
$base_folder=realpath($tcn_img_folder);
$tcn_img_folder.="/";
}else{
$base_folder=$this_folder;
}
$tcn_images=array();
$folders=array();
if($browse_subdirs=="true"){
$folders=list_folders($base_folder);
}
array_push($folders,$base_folder);
foreach($folders as $folder){
chdir($folder);
$handle=opendir('.');
while(false !== ($file = readdir($handle))){
if (strtolower(substr($sub_dir."/".$file, -3)) == "gif" || strtolower(substr($sub_dir."/".$file, -3)) == "jpg" || strtolower(substr($sub_dir."/".$file, -4)) == "jpeg" || strtolower(substr($sub_dir."/".$file, -3)) == "png"){
array_push($tcn_images,$folder."/".$file);
}
}
}
if(count($tcn_images)>0){
srand ((double) microtime() * 1000000);
$tcn_image=$tcn_images[rand(0,sizeof($tcn_images) - 1)];
$tcn_image_dimensions = getimagesize($tcn_image);
$dimensions=$tcn_image_dimensions[3];
$tcn_image=$tcn_img_folder.substr(($tcn_image),strlen($base_folder)+1);
echo "\"".$tcn_image."\" ".$dimensions;
}
chdir($this_folder);
}
function list_folders($basedir, $all_folders=array()) {
$this_folder=array();
chdir($basedir);
$current=getcwd();
$handle=opendir(".");
while ($file = readdir($handle)) {
if (($file!='..') & ($file!='.')) {
if (is_dir($file)) {
array_push($this_folder,$current.'/'.$file);
}
}
}
closedir($handle);
foreach ($this_folder as $key=>$var) {
array_push($all_folders, $var);
$all_folders=list_folders($var, $all_folders);
}
chdir($basedir);
return $all_folders;


THE HTML CODE (sits on the same page as the PHP code)

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="248"><img src=<?php random_image("","/images/photobar/left") ?>></td>
<td width="2" class="BGLightThinLine"><img src="/images/misc/s.gif" width="2" height="1"></td>
<td width="248"><img src=<?php random_image("","/images/photobar/middle") ?>></td>
<td width="2" class="BGLightThinLine"><img src="/images/misc/s.gif" width="2" height="1"></td>
<td width="248"><img src=<?php random_image("","/images/photobar/right") ?>></td>
<td width="2" class="BGLightThinLine"><img src="/images/misc/s.gif" width="2" height="1"></td>
<td width="1" class="BGDarkThinLine"><img src="/images/misc/s.gif" width="1" height="1"></td>
<td class="BGPage"> </td>
</tr>
</table>
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

you got your php_include_path set wrong... check out your php.ini file.. the include works from the document root.. so if you move your script to a subfolder, you'll have to add that folder to the include_path directive.. (i think ./ works also...)

http://www.php.net/manual/en/configurat ... clude-path
phpadam
Forum Newbie
Posts: 5
Joined: Wed Dec 10, 2003 9:58 am

Post by phpadam »

Thank you for your response. I have sent an email to the support dept. of my hosting provider, requesting that they modify the include_path directive to include ./ per your suggestion. I'll let you know if it works.

However, I'm a bit confused: If I'm able to get the rest of my php-included files showing up correctly on pages residing in subdirectories, why isn't it working with the file with the php script in question? In fact, the HTML shows up fine from this file; it's just the scipt that's breaking. How is the script interacting with my directory structure such that it won't work without your suggested modification to the include_path directive?

Learning all the time...
Adam
phpadam
Forum Newbie
Posts: 5
Joined: Wed Dec 10, 2003 9:58 am

Post by phpadam »

Okay, here's the response from tech support at the hosting company:

"Sorry, that is not an option we can change in the ini file, it would adversely effect to many other sites. Put your includes in a single folder and call from there, will work much better."

All my includes are in a single folder (called 'includes') so I'm still at a loss for how I modify the script so that it will work as desired. Thoughts?
phpadam
Forum Newbie
Posts: 5
Joined: Wed Dec 10, 2003 9:58 am

Post by phpadam »

One additional note: I'm including the file that contains the code listed above using the following syntax:

<? require ("http://65.110.91.154/includes/head.html"); ?>

I'm using the full URL because relative URLS weren't working (I still don't understand why). I'm using 'require' instead of 'include', because it's my understand that this will result in the php script running only after it's been included on the page. Is this faulty thinking on my part?
phpadam
Forum Newbie
Posts: 5
Joined: Wed Dec 10, 2003 9:58 am

Post by phpadam »

Sorry for the long string of posts. I figured it out. Though /images/... wasn't working as far as designation of the image directory that the script should look in, ../../../../images does work. It's not as pretty, but at least it gets the job done. Thanks again for looking it over.
Post Reply