Random Image Script Not Working With Virutal Paths
Posted: Wed Dec 10, 2003 9:58 am
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>
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>