I have found this forum while searching for an answer to a problem I have with my PHP code.
Basically I want to be able to display images images from a directory on the top of my page. I found the code I am about to show you somewhere on the net.
I created a php file and called it "getimages.php" the code for this is below I have placed this file in the same directory as the images.
- Code: Select all
<?
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");
//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname=".") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";';
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
I then added this code to the part of the page where I want the images to display. you can see I changed the first line "upload/getimages.php" to specify the directory where my images are located
- Code: Select all
<script src="upload/getimages.php"></script>
<script type="text/javascript">
var curimg=0
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "pics/"+galleryarray[curimg])
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
setInterval("rotateimages()", 2500)
}
</script>
<div style="width: 170px; height: 160px">
<img id="slideshow" src="upload/bear.gif" />
</div>
When I upload all this to my server all I see is the icon for a broken image. Can anyone here tell me where I have gone wrong. Im not sure but for me to get a broken image icon I thought it might have had something to do with the path I specified rather than the PHP code it'self and yes I have also uploaded an image called bear.gif.
Any guidance appreciated
cheers Nige.
ps I can supply the website address of where this code came from if you think it will help

