Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:46:08+00:00 2026-06-15T01:46:08+00:00

Basically, I have a very simple website where the root directory looks like: /images/

  • 0

Basically, I have a very simple website where the root directory looks like:

/images/
index.html
stuff.js

I want some way to recursively iterate through every file in the /images/ directory and display them in order in a section of my website. So for example, if /images/ contained:

images/a/a.png
images/b.png
images/c.jpg
....

then somewhere in index.html would contain:

<img src="images/a/a.png" />
<img src="images/b.png" />
<img src="images/c.jpg" />
....

My first idea was to do this using the document.write() function in stuff.js, but I couldn’t find a good way to iterate through the local file directory in Javascript. I saw something about AJAX, but all of those examples involved editing an existing file, which I obviously don’t want to do.

My current solution is just to manual create an array of strings containing all of the files in /images/, but doing this makes me think “There’s got to be a better way!”

Let me know if I’ve been unclear.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:46:10+00:00Added an answer on June 15, 2026 at 1:46 am

    Perhaps the best way to do this is to use a server-sided language to do it for you, and to use an asynchronous Javascript request to display the data.

    This sample uses PHP to list all the files in a specified directory, and an xmlhttprequest to load this output and convert the results into image tags:


    getimages.php:

    <?php
    
        //The directory (relative to this file) that holds the images
        $dir = "Images";
    
    
        //This array will hold all the image addresses
        $result = array();
    
        //Get all the files in the specified directory
        $files = scandir($dir);
    
    
        foreach($files as $file) {
    
            switch(ltrim(strstr($file, '.'), '.')) {
    
                //If the file is an image, add it to the array
                case "jpg": case "jpeg":case "png":case "gif":
    
                    $result[] = $dir . "/" . $file;
    
            }
        }
    
        //Convert the array into JSON
        $resultJson = json_encode($result);
    
        //Output the JSON object
        //This is what the AJAX request will see
        echo($resultJson);
    
    ?>
    

    index.html (same directory as getimages.php):

    <!DOCTYPE html>
    <html>
        <head>
            <title>Image List Thing</title>
        </head>
        <body>
    
            <div id="images"></div>
            <input type="button" onclick="callForImages()" value="Load" />
    
            <script>
    
                //The div element that will contain the images
                var imageContainer = document.getElementById("images");
    
    
    
                //Makes an asynch request, loading the getimages.php file
                function callForImages() {
    
                    //Create the request object
                    var httpReq = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    
                    //When it loads,
                    httpReq.onload = function() {
    
                        //Convert the result back into JSON
                        var result = JSON.parse(httpReq.responseText);
    
                        //Show the images
                        loadImages(result);
                    }
    
                    //Request the page
                    try {
                        httpReq.open("GET", "getimages.php", true);
                        httpReq.send(null);
                    } catch(e) {
                        console.log(e);
                    }
    
                }
    
    
                //Generates the images and sticks them in the container
                function loadImages(images) {
    
                    //For each image,
                    for(var i = 0; i < images.length; i++) {
    
                        //Make a new image element, setting the source to the source in the array
                        var newImage = document.createElement("img");
                        newImage.setAttribute("src", images[i]);
    
                        //Add it to the container
                        imageContainer.appendChild(newImage);
    
                    }
    
                }
    
                </script>
    
        </body>
    </html>
    

    Note that this is only an example. You’ll probably want to make sure that the AJAX call is successful, and that the JSON conversion works both in the server code and on the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple mapping which looks like this (I streamlined the example
My question is very simple, I basically have 2 apps that want to access
I have a very simple web synchronization PHP I want to build which basically
i have some very simple js (i'm still learning) that basically reads the elements
I have a very simple usercontrol, basically a textbox and a label, whose purpose
I have a very simple agent, basically just the required Agent_OnLoad method signature. If
We have a very simple page for displaying the records. Basically, it passes certain
I have built a very simple CakePHP website using the Auth component and have
I have a relatively simple case. I basically want to store data about links
New to using Backbone and have a very simple application. Basically there are Clients

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.