Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

displaying images from directory across the screen

images php coding issues or problems here.

Moderators: macek, egami, gesf

displaying images from directory across the screen

Postby Nigel » Sun Sep 30, 2012 3:26 am

Hi,
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
Nigel
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Sep 30, 2012 2:59 am

Re: displaying images from directory across the screen

Postby Nigel » Sun Sep 30, 2012 3:54 am

This always happens, OK the code is working to some degree I had specified a bad path.
Anyway what is happening know is it is displaying an image the first one in my images directory. I was thinking great but the image does not change? I wanted it to change between all the images in my directory. Any ideas?
Nigel
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Sep 30, 2012 2:59 am


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.