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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:12:35+00:00 2026-05-24T00:12:35+00:00

function file_list($path){ $final_result=array(); if ($handle = opendir($path)) { while (false !== ($file = readdir($handle)))

  • 0
function file_list($path){
    $final_result=array();
    if ($handle = opendir($path)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (is_dir($path."/".$file)) {
                    //echo $path."/".$file."\n";//directory
                    return file_list($path."/".$file); 
                } else {
                    //echo $path."/".$file."\n"; //all file 
                    if(stripos($path."/".$file,"playlist.data"))
                    {
                        //echo $path."/".$file."\n"; //file contains "list.data"
                        $content = file_get_contents($path."/".$file);
                        preg_match_all("/([0-9]*).txt,0/",$content, $matches);
                        $result=array_unique($matches[1]);
                        $final_result=array_merge($final_result,$result);
                        $final_result=array_unique($final_result);
                        sort($final_result);
                        return($final_result);
                    }
                }
            } 
        } 
    } 
} 
print_r(file_list($argv[1]));

list.data file like this:

1.txt
3.txt

another list.data file like this:

2.txt
4.txt

so the result should be array like this:

Array(
    [0]=>1
    [1]=>2
    [2]=>3
    [3]=>4
)

I was aim to search the specified directory of the file system recursively,when I found file name called “list.data”, I read this file and using regex to filter and array to $result. since I have more than one “list.data”, I wannted to merge every $result to $final_result. but this script output nothing.
can anybody tell me does something wrong with file_list function.

i am execute this php script in CLI mode like this:
php.exe script.php “d:/test/listdir”

  • 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-24T00:12:35+00:00Added an answer on May 24, 2026 at 12:12 am

    This is a paraphrased pseudo-code version showing the recursive merging logic:

    function file_list($path) {
        $result = array();
    
        foreach ($path as $file) {
            if (is_dir($file)) {
                $result = array_merge($result, file_list($file));
            } else {
                $result[] = ...
            }
        }
    
        return $result;
    }
    

    The function always returns an array, even if it’s empty. Going through a directory, you add your results into the $result array. If you encounter a subdirectory, you let file_list go through it, merging the results into your current $result array (because file_list returns an array of results). Finally, after all entries in the directory have been handled, you return the list of results to the caller (which may be file_list itself).

    If you feel like experimenting, look into the RecursiveDirectoryIterator, which would allow you to flatten the logic in your function, making it non-recursive.

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

Sidebar

Related Questions

The function below takes a python file handle, reads in packed binary data from
I have some jQuery code. I have called an Ajax function file, file.php, that
Suppose a header file defines a function template. Now suppose two implementation files #include
I have a function that automatically exports a table into a CSV file, then
I have this function to read in all ints from the file. The problem
I have a bash script file which starts with a function definition, like this:
I've written this very simple function to replace a file extension using LINQ in
I have a function called: void initializeJSP(string Experiment) And in my MyJSP.h file I
I need to find all occurrences of a function call in a C++ file
I have a parser function written in my code-behind file, that takes as input

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.