0

I have a directory with two files, i whant to store the file names into array,bath when i open the directory and tray to store the file names i am geting two arrays,one [0] => may.log and second [0] => may.log, [1] => april.log. Hier is my code:

    <?php       
        $dir = 'data';
        $fileNames = array();
        if(is_dir($dir)){
            $handle = opendir($dir);
            while(false !== ($file = readdir($handle))){
                if(is_file($dir.'/'.$file) && is_readable($dir.'/'.$file)){
                        $fileNames[] = $file;
                        $fileNames = array_reverse($fileNames);
                        print_r($fileNames);

                }
            }
            closedir($handle);
        }else {
            echo "<p>There is an directory read issue</p>";
        }
    ?>
5
  • 3
    Use glob e.g. $files = glob("data/*.*"); And you all have it in a array! (See: php.net/manual/en/function.glob.php) Commented Feb 8, 2015 at 9:51
  • 1
    @Rizier123 Never tried it, but shouldn't it be glob("data/*")? File names don't have to contain an extension and even if they do, * should still match the whole name, right? Commented Feb 8, 2015 at 10:05
  • @still_learning on unix you are right, but * will also match folders. Valid files (in windows) have an extension. So, *.* would theoretically only return files, rather than files and folders. (assuming all the files HAVE an extension, which they mostlikely do have if they are served for download.) Even if the server is unix, I never saw anybody remoing extensions like .pdf, .zip just because they are not required. Commented Feb 8, 2015 at 10:08
  • @still_learning but at the end you are right. Conventions are good, control is better: use * and check if its a file or folder when processing elements. (is_file() and is_dir()) Commented Feb 8, 2015 at 10:13
  • @dognose I think FAT16 was the last FS which actually enforced extensions. Every modern filesystem (even on Windows, tried it) allows file names without extension and folders with an "extension". Commented Feb 8, 2015 at 10:15

1 Answer 1

3
  1. Move out array_reverse and print_r from loop

    $dir = 'data';
    $fileNames = array();
    if(is_dir($dir)){
        $handle = opendir($dir);
        while(false !== ($file = readdir($handle))){
            if(is_file($dir.'/'.$file) && is_readable($dir.'/'.$file)){
                    $fileNames[] = $file;
            }
        }
        closedir($handle);
        $fileNames = array_reverse($fileNames);
        print_r($fileNames);
    }else {
        echo "<p>There is an directory read issue</p>";
    }
    
  2. Replace this code with glob (like @Rizier said)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank's Sectus for your help, i move array_reverse and print_r and now i have just one array , thanks again:).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.