2012-11-04 35 views
-2

我正在嘗試使用Javascript和PHP來讀取圖像的文件夾並循環顯示圖像。 我從這裏得到了想法和代碼:http://www.javascriptkit.com/javatutors/externalphp2.shtml外部JavaScript和PHP通過圖像文件夾旋轉

即使使用Firebug,我也沒有收到任何錯誤。但它只是不通過圖像循環。它加載我設置的第一個圖像(userfiles/portfolio/0025.jpg)。但永遠不會移動到另一個圖像。

getimages.php位於userfiles/portfolio以及我的圖像。

這是我有:

在我想要顯示的圖像的頁面:

<script src="userfiles/portfolio/getimages.php"></script> 

<script type="text/javascript"> 

var curimg=0 
function rotateimages(){ 
document.getElementById("slideshow").setAttribute("src", "userfiles/portfolio/"+galleryarray[curimg]) 
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0 
} 

window.onload=function(){ 
setInterval("rotateimages()", 1000) 
} 
</script> 

<div id="portfoilo"> 
    <img id="slideshow" src="userfiles/portfolio/0025.jpg" /> 
</div> 

,這裏是我的getimages.php

<?php 
//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 
?> 

getimages.php被顯示出來當我跑它時,空白。但是現在我得到:

<font size='1'><table class='xdebug-error xe-deprecated xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'> 
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr> 
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Deprecated: Function eregi() is deprecated in J:\My Documents\Dropbox\www\ChiseledImages-Ras\userfiles\portfolio\getimages.php on line <i>13</i></th></tr> 
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> 
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> 
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0014</td><td bgcolor='#eeeeec' align='right'>248760</td><td bgcolor='#eeeeec'>{main}()</td><td title='J:\My Documents\Dropbox\www\ChiseledImages-Ras\userfiles\portfolio\getimages.php' bgcolor='#eeeeec'>..\getimages.php<b>:</b>0</td></tr> 
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>249536</td><td bgcolor='#eeeeec'>returnimages()</td><td title='J:\My Documents\Dropbox\www\ChiseledImages-Ras\userfiles\portfolio\getimages.php' bgcolor='#eeeeec'>..\getimages.php<b>:</b>26</td></tr> 
</table></font> 

eregi現在完全消失了嗎?我知道它已被棄用後5和我的WAMP版本運行PHP 5.4.3。

+1

什麼問題?你有錯誤嗎? –

+0

是的,你的問題是什麼? – rudolph9

+0

大聲笑。如果我說出了什麼問題,猜猜這會有所幫助。 即使使用Firebug,我也沒有收到任何錯誤。它只是不通過圖像循環。它加載我設置的第一個圖像(userfiles/portfolio/0025.jpg)。但永遠不會移動到另一個圖像。 –

回答

1

只要你的代碼的快速一瞥,但你或許應該改變這樣的:

setInterval("rotateimages()", 1000) 

這個

setInterval(rotateimages, 1000); 

這是因爲你告訴的Javascript 1後執行特定的功能第二。所以你需要傳遞一些可調用的,如函數引用或匿名函數塊。在你的情況下,你傳遞的字符串不是,可以調用

+0

感謝您的快速反應,但我仍然得到相同的結果。 –

+0

嘗試在您的代碼中放置提醒,以查看哪些內容未觸發。另外,您應該編輯您的答案以包含PHP腳本輸出的內容。 –

+0

我之前沒有收到任何輸出。現在我得到了上面的內容。我看到eregi已被棄用的php錯誤。我假設,因爲我在PHP 5.4.3和eregi已被否決5之後,這是造成這個問題? 我應該用stristr()來嘗試嗎? –