gpt4 book ai didi

php - php : read filenames, 生成 javascript 和 html 时出现问题

转载 作者:行者123 更新时间:2023-11-28 10:21:52 26 4
gpt4 key购买 nike

更新

你好。我发现自己遇到了一个新问题。 php代码在我的PC(wamp服务器)上完美运行,但我现在已将其上传到免费的网络主机服务器上,虽然php部分运行完美(它生成数组),但javascript函数本身不起作用,因为没有照片在网站加载时。我尝试通过在函数的第一行添加一个警报来测试它,看看它是否运行但从未出现。我认为服务器由于某种原因没有意识到它是一个 javascript 函数,因为我在 getphotos.php 中也有这样的:

window.onload = photos();

除了启动照片功能外,还显示文本。当我在 js 文件中移动该行并将显示文本行放在第一位时,它会运行显示文本,但仍然没有照片。你觉得怎么样????

更新结束

<小时/>

大家好。我正在建立一个显示一些照片的网站。我希望网站通过读取照片文件夹中的文件名自动生成显示照片的 html 代码,但我还需要使用 javascript。所以我通过网络找到了一个用 php 生成 javascript 的解决方案,它可以生成我想要的 html 代码,我认为这就是我需要的。但是...它不起作用X_X。所以我需要有人的帮助!

首先,这是 php/javascript(在 getPhotos.php 中):

<?
header("content-type: text/javascript");

//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnImages() {
$pattern="(*.jpg)|(*.png)|(*.jpeg)|(*.gif)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir('/photos/')) {
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);
}

//here starts the javascript function
echo 'window.onload = photos;
function photos(){
var i;
var text1 = "";
var text2 = "";
var text3 = "";
var galleryArray=new Array();'; //Define array in JavaScript
returnImages(); //Output the array elements containing the image file names

//short the images in three sets depending on their names and produce the code
echo 'for(i=0; i<galleryArray.length; i++){
if(galleryArray[i].indexOf("set1_")!=-1){
text1+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
}else if(galleryArray[i].indexOf("set2_")!=-1){
text2+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
}else if(galleryArray[i].indexOf("set3_")!=-1){
text3+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
}
}';

//create text nodes and put them in the correct div
echo 'var code1 = document.createTextNode(text1);
var code2 = document.createTextNode(text2);
var code3 = document.createTextNode(text3);
document.getElementById("galleryBox1").appendChild(code1);
document.getElementById("galleryBox2").appendChild(code2);
document.getElementById("galleryBox3").appendChild(code3);
}';

?>

这是鬃毛页面index.html中的代码:

<script type="text/javascript" src="getPhotos.php"></script><!--get photos from dir-->

就是这样,但行不通!我知道我通过提供所有代码并寻求帮助来要求很多,但我什至不知道出了什么问题,更不用说如何修复它了......所以请,如果你有任何想法,那就太好了。

最佳答案

; returnImages() 后缺失。

This function (readdir) may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

http://php.net/manual/en/function.readdir.php

所以尝试使用 while(false != ($file = readdir($handle))){while(FALSE !== ($file = readdir($handle))){

关于php - php : read filenames, 生成 javascript 和 html 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com