0

I have a PHP script that loads all images in a hard coded folder into JavaScript array:

<?
//PHP SCRIPT: readphp.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|jpeg|png|gif)$/i"; //valid image extensions
    $files = array();
    $curimage=0;
    if($handle = opendir($dirname)) {
        while(false !== ($file = readdir($handle))){
            if(preg_match($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
?>

I call this script in an HTML file like this:

<script src="readphp.php"></script>

Then I use galleryarray in other <script>.

Now I want to expand my application with the following logic:

User will pass (never mind the details) a folder path. Then the path is passed to the PHP script which will load all images in that folder. I do something with these images and then the user will be asked to supply another folder path and so on and so forth.

How can I do that?

user1692261
  • 1,197
  • 3
  • 16
  • 30
  • PHP scripts aren't supposed to be run from script tags (?) [See this SO question](https://stackoverflow.com/questions/22853669/how-can-i-run-a-php-script-inside-a-html-file) – Rojo Feb 17 '21 at 16:13
  • @Rojo I use PHP because I didn't find any better way to load all images in a folder into HTML. It works just fine :X – user1692261 Feb 17 '21 at 16:16

0 Answers0