正在努力创建一个php数组来从可以用作JavaScript数组的目录中获取照片
基本上,我正在尝试创建一个照片幻灯片,根据用户ID显示特定的照片。这些照片将存储在我的web服务器空间的目录中。目前,我有一个html没有变成php文件,带有基本的html布局、css样式表和一个外部js文件,其中包含我的代码,可以使照片淡入淡出。我在html的底部添加了php。这就是我所拥有的:正在努力创建一个php数组来从可以用作JavaScript数组的目录中获取照片,javascript,php,jquery,Javascript,Php,Jquery,基本上,我正在尝试创建一个照片幻灯片,根据用户ID显示特定的照片。这些照片将存储在我的web服务器空间的目录中。目前,我有一个html没有变成php文件,带有基本的html布局、css样式表和一个外部js文件,其中包含我的代码,可以使照片淡入淡出。我在html的底部添加了php。这就是我所拥有的: $user_id = $_GET['userid']; print "<h1> Hi, $user_id </h1>"; function returnimages($di
$user_id = $_GET['userid'];
print "<h1> Hi, $user_id </h1>";
function returnimages($dirname = "Photos/1") { //will replace 1 with userid once something starts working
$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
?>
我现在已将其注释掉并替换为以下内容:
var userphoto = <? echo json_encode($galleryarray); ?>;
我希望能够使用新阵列更改photodisplay的src:
photodisplay[x].attrsrc,userphoto[x]
对不起,如果我的问题不清楚的话。我自己也很困惑希望有人能帮忙
$user_id = (int) $_GET['userid'];
print "<h1> Hi, $user_id </h1>";
function returnimages($dirname = "Photos/1") {
$dirname = str_replace('..', '.', $dirname); //only remove this if you know why it's here
$pattern = "*{.jpg,.png,.jpeg,.gif}"; //valid image extensions
return glob($dirname . DIRECTORY_SEPARATOR . $pattern, GLOB_BRACE);
}
echo "var galleryarray = ".json_encode(returnimages()).";\n";
?>
另外,您应该首先使用PHP短标记,您是否设置了PHP调试?您是否能够验证服务器是否正在获取所描述的文件?第二,直接混合使用PHP和Javascript是个坏主意。最后,我建议将文件数组输出为return json_encode$files.Hi Jason,谢谢您的回复!不,我不知道,我认为服务器正在获取文件,因为它在页面底部打印出数组;galleryarray[0]=1.jpg;galleryarray[1]=2.jpg;我很困惑:
$user_id = (int) $_GET['userid'];
print "<h1> Hi, $user_id </h1>";
function returnimages($dirname = "Photos/1") {
$dirname = str_replace('..', '.', $dirname); //only remove this if you know why it's here
$pattern = "*{.jpg,.png,.jpeg,.gif}"; //valid image extensions
return glob($dirname . DIRECTORY_SEPARATOR . $pattern, GLOB_BRACE);
}
echo "var galleryarray = ".json_encode(returnimages()).";\n";
?>