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 6539143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:49:21+00:00 2026-05-25T10:49:21+00:00

I am trying to create an a list of files within a directory, including

  • 0

I am trying to create an a list of files within a directory, including those in subdirectories, showing their full path, filename and extension. Any guidance would be appreciated.

After plenty of reading I have attempted several things, this is the most recent I found and tried but have not had any joy, for now I am just looking for the browser to display the above information.

$dir = '../test/images/';
function getFileList($dir, $recurse=false)
{
    // array to hold return value
    $retval = array();
    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";
    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
        // skip hidden files
        if($entry[0] == ".") continue;
        if(is_dir("$dir$entry")) {
            $retval[] = array(
                "name" => "$dir$entry/",
                "type" => filetype("$dir$entry"),
                "size" => 0,
                "lastmod" => filemtime("$dir$entry")
            );
            if($recurse && is_readable("$dir$entry/")) {
                $retval = array_merge($retval, getFileList("$dir$entry/", true));
            }
            } elseif(is_readable("$dir$entry")) {
            $retval[] = array(
                "name" => "$dir$entry",
                "type" => mime_content_type("$dir$entry"),
                "size" => filesize("$dir$entry"),
                "lastmod" => filemtime("$dir$entry")
            );
        }
    }
    $d->close();
    return $retval;
}
print_r($retval);

My ultimate goal is to create some php that on execute will look in a directory, create the subdirectory (and any subdirectories there in) with the same name in a new directory (so /test/album/ would be created as /live/album/). Then resize any images that are contained in each original directory and save them to their related new directory. I appreciate this is a lot and will no doubt have many questions as I proceed, but will save those for later.

  • 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-05-25T10:49:21+00:00Added an answer on May 25, 2026 at 10:49 am

    Here’s a similar script I wrote for my sites:

    <?php
    $baseDir = '/images/test';
    $newDir = '/images/live/';
    $returnFiles = array();
    
    function getDirectory($path, $level, $newDir, $baseDir, &$returnFiles) {
        if(substr($path, -1) != DIRECTORY_SEPARATOR) {
            $path = $path . DIRECTORY_SEPARATOR;
        }
        if(substr($newDir, -1) != DIRECTORY_SEPARATOR) {
            $newDir = $newDir . DIRECTORY_SEPARATOR;
        }
        if(substr($baseDir, -1) != DIRECTORY_SEPARATOR) {
            $baseDir = $baseDir . DIRECTORY_SEPARATOR;
        }
        $dh = opendir( $path );
        if(!$dh) {
            return;
        }
        while( false !== ( $file = readdir( $dh ) ) ) {
            if($file == '.' || $file == '..') {
                continue;
            }
            $ident = str_repeat("\t", $level);
    
            if(is_file($path . $file)) {
                if(substr(mime_content_type($path . $file), 0, 5) == 'image') {
                    $returnFiles[] = saveImage($path . $file, $newDir, $baseDir);
                }
            }
            else if(is_dir($path . $file)) {
                getDirectory($path . $file . DIRECTORY_SEPARATOR, $level+1, $newDir, $baseDir, $returnFiles);
            }
        }
        closedir( $dh );
    } 
    
    function saveImage($image, $newDir, $baseDir) {
        $current = dirname($image) . DIRECTORY_SEPARATOR;
        $path = str_replace($baseDir, $newDir, $current);
        if(!is_dir($path)) {
            mkdir($path, 0777, TRUE);
        }
        $oldImage = array(
            "name" => $image,
            "type" => mime_content_type($image),
            "size" => filesize($image),
            "lastmod" => filemtime($image)
        );
        // conver image
        convertImage($image, $path);    
        $image = $path . basename($image);
        $newImage = array(
            "name" => $image,
            "type" => mime_content_type($image),
            "size" => filesize($image),
            "lastmod" => filemtime($image)
        );
        return array('old' => $oldImage, 'new' => $newImage);
    }
    
    function convertImage($image, $folder) {
        // do what you need to do to convert/resize etc..
        // for demo purpose I simply copy the image.
        copy($image, $folder . basename($image));
    }
    
    getDirectory($baseDir, 0, $newDir, $baseDir, $returnFiles);
    print_r($returnFiles);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to traverse a given directory and create a list of files
i'm trying to create downloadable video-files. In my site there is a list of
I am trying to create a directory to store my application's files in the
I am currently trying to zip all files within a directory. The zip file
I am trying to create a list of all virtual directories within an IIS
I am trying to create a list where I want users to enter data
I'm trying to create a list of permutations of a list, such that, for
I'm trying to create UL list with images and descriptions. With 5 items in
I'm trying to create a list, with each <li> on one row. My problem
I'm trying to create a list of tasks that I've read from some text

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.