我正在尝试使用 Javascript 和 PHP 来读取图像文件夹并循环浏览图像。我从这里得到了想法和代码:http ://www.javascriptkit.com/javatutors/externalphp2.shtml
即使 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。