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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:28:44+00:00 2026-06-08T12:28:44+00:00

I am sorry if this question is obvious for the ninjas, but I am

  • 0

I am sorry if this question is obvious for the ninjas, but I am quite a novice in PHP and I am struggling with it all day ..

I am trying to get a list of all files from a folder structure.

Currently it gives me something like

Array([0]->path/filename [1]->path/filename) Array([0]->path/filename [1]->path/filename..)

(one for each folder)

function o99_list_all_files_in_dir($dir) //need to ocheck for server compatibility (unix,linux,win)
{ 

    $root = scandir($dir); 
    foreach($root as $value) 
    { 
        if($value === '.' || $value === '..') {continue;} 

        if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;} 

    //if(is_file("$dir/$value")) {$result["$dir"]="$value";continue;} 

        foreach(k99_list_all_files_in_dir("$dir/$value") as $value) 
        { 
            $result[]=$value; 

        //$result["$dir"]=$value; 
        }
    } 

    //print_r($result);
    return $result; 
} 

Few questions :

1 – I need both the path and the filename pair so I thought to get an array like so :

results([path] -> [filename] [anotherpath] -> [anotherfilename]).

but if I try to construct another array (switch uncomment and comment lines) the function will give me only the 1st file in each dir.

2 – Later on , I am using this function in order to have both the path and filename seperated , So I tried this :

$result = o99_list_all_files_in_dir($upload_dir);   
                foreach ($result as $image) {
                 reset($image);
                 while (list($key, $val) = each($image)) {
                    // echo "$key => $val\n";
                 }
                $filename = pathinfo($image);// I need the path here ...
                         ...

… but obviously it is not working (otherwise I would not be here 🙂

3 – Bonus question : How can I filter files from the results (like thumbs.db for example) or decide how to ignore or not certain extensions ??

EDIT I
4 – !important (forgot before) – what do I need to be careful about when dealing with unknows server paths (Linux, Win, Unix) …will this function work on all ?

  • 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-08T12:28:45+00:00Added an answer on June 8, 2026 at 12:28 pm

    Try this code:

    function scanFileNameRecursivly($path = '', &$name = array() )
    {
      $path = $path == ''? dirname(__FILE__) : $path;
      $lists = @scandir($path);
    
      if(!empty($lists))
      {
          foreach($lists as $f)
          { 
              if(is_dir($path.DIRECTORY_SEPARATOR.$f) && $f != ".." && $f != ".")
              {
                  scanFileNameRecursivly($path.DIRECTORY_SEPARATOR.$f, &$name); 
              }
              else
              {
                  $name[] = $path.DIRECTORY_SEPARATOR.$f;
              }
          }
      }
      return $name;
    }
    
    $path = "abs path to your directory";
    $file_names = scanFileNameRecursivly($path);
    
    echo "<pre>";
    var_dump($file_names);
    echo "</pre>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry if this is an obvious question, but I'm rather new
All Sorry if this is an obvious question but does WCF streaming work correctly
Sorry if this is an obvious question, but I've found surprisingly few references on
Sorry if this is a question with a obvious answer but my knowledge with
I'm sorry if this is an obvious question but neither Google or a search
Sorry if this question seems obvious for everyone, but I am very new to
Sorry if this is an obvious question, but some preliminary googling came up with
Sorry if this is an obvious question, but my knowledge of asp.net is a
sorry if this is an obvious question, but I am relatively new to python
Sorry to ask this question, but it's now day 3 I try to solve

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.