php的问题:读取文件名,生成javascript和html [英] problem with php: read filenames, generate javascript and html

查看:65
本文介绍了php的问题:读取文件名,生成javascript和html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello again. I found myself with a new problem. The php code worked perfectly on my PC (wamp server) but i've now uploaded it on a free webhost server and while the php part runs perfectly (it produces the array) the javascript function itself doesn't work cause there are no photos in the website when it's loaded. I tried to test it by putting in the function's first line an alert to see if it runs but never showed up. I think that the server for some reason doesn't realise that it is a javascript function because i also had in the getphotos.php this:

window.onload = photos();

启动照片功能的appart显示文字。当我在js文件中移动该行并将显示文本行放在第一行时,它会显示文本但仍然没有照片。您怎么看?

which appart from starting the photos function, shows a text. When i moved that line in js file and put the show text line first, it run showing the text but still no photos. What do you think????

更新结束

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

Hello to everyone. I am building a website that shows some photos. I want the site to automatically generate the html code that shows the photos by reading the file names in the photo folder, but i need also to use javascript. So I found through the web a solution with php generating javascript which than generates the html code I want and I think this is what I need. But... it doesn't work X_X. So I need someone's help!

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

Firstly, here is the php/javascript(in 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中的代码:

And this is the code in the mane page index.html:

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

这就是它,它不起作用!我知道我只是通过提供所有代码并寻求帮助来提出更多要求,但我甚至无法想到什么是错的,更不用说如何解决它了......所以,如果您有任何想法它会很棒。

This is it, and it doesn't work! I know I ask to much by just giving all the code and asking for help but i can't even think what's wrong, let alone how to fix it.... So please, if you have any idea it would be great.

推荐答案

; returnImages()之后缺失。


此函数(readdir)可能返回布尔值
FALSE,但也可能返回
非布尔值,其值为
FALSE,例如0或。请阅读
关于布尔值的部分,了解更多
的信息。使用===运算符为
测试此
函数的返回值。

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的问题:读取文件名,生成javascript和html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆