problema con php: leer nombres de archivos, generar javascript y html

ACTUALIZA

Hola de nuevo. Me encontré con un nuevo problema. El código php funcionó perfectamente en mi PC (servidor wamp) pero ahora lo he subido a un servidor webhost gratuito y aunque la parte php se ejecuta perfectamente (produce la matriz), la función javascript no funciona porque no hay fotos en el sitio web cuando está cargado. Traté de probarlo poniendo en la primera línea de la función una alerta para ver si se ejecuta pero nunca apareció. Creo que el servidor por alguna razón no se da cuenta de que es una función de JavaScript porque también tenía en getphotos.php esto:

window.onload = photos();

que además de iniciar la función de fotos, muestra un texto. Cuando moví esa línea en el archivo js y puse la línea de mostrar texto primero, se ejecuta mostrando el texto pero todavía no hay fotos. ¿¿¿¿Qué piensas???

FIN DE ACTUALIZACIÓN

Hola a todos. Estoy construyendo un sitio web que muestra algunas fotos. Quiero que el sitio genere automáticamente el código html que muestra las fotos leyendo los nombres de archivo en la carpeta de fotos, pero también necesito usar JavaScript. Entonces encontré a través de la web una solución con php que genera javascript que luego genera el código html que quiero y creo que esto es lo que necesito. Pero ... no funciona X_X. ¡Entonces necesito la ayuda de alguien!

rimero, aquí está el php / javascript (en 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);
}';

?> 

Y este es el código en la página mane index.html:

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

¡Esto es todo, y no funciona! Sé que pido mucho simplemente dando todo el código y pidiendo ayuda, pero ni siquiera puedo pensar qué está mal, y mucho menos cómo solucionarlo ... Entonces, por favor, si tienes alguna idea, sería genial.

Respuestas a la pregunta(2)

Su respuesta a la pregunta