php get full path of file in a folder

2020-02-13 03:21发布

问题:

I have a folder which named data. In this data folder, there are files and folders. In this folders there is files and folders.... Continues like this...

I need to get all file path's (include sub directories' files). I have a php code:

function dirToArray($dir) {
    $contents = array();
    foreach (scandir($dir) as $node) {
        if ($node == '.' || $node == '.htaccess' || $node == '..' || $node == '.tmb' || $node == '.quarantine') continue;
        if (is_dir($dir . '/' . $node)) {
            $contents[$node] = dirToArray($dir . '/' . $node);
        } else {
            $contents[] = $node;
        }
    }
    return $contents;
}
$fileData = dirToArray( '/var/www/image/data/' );
print_r($fileData);

The output is on this link. It gives me a tree of data folder like this:

Array
(
    [04.2014] => Array
        (
            [C] => Array
                (
                    [0] => AMANFTR105260005_001.jpg
                    [Acki] => Array
                        (
                            [0] => 10269990_1407183112891667_859040604_n.jpg
                            [1] => 10287174_1407183106225001_914722369_n.jpg
                            [2] => 10307039_1407183109558334_889879385_n.jpg
                            [3] => 10318614_1407183099558335_776826424_n.jpg
                            [25763] => Array
                                (
                                    [0] => 10268126_1407184372891541_1399955485_n.jpg
                                    [1] => 10268256_1407184362891542_462829886_n.jpg
                                    [2] => 10318854_1407184356224876_1056593541_n.jpg
                                    [3] => photo.jpg
                                )

                            [73085] => Array
                                (
                                    [0] => 10261927_1407212376222074_295083908_n.jpg
                                    [1] => 10268368_1407212366222075_706285245_n.jpg
                                    [2] => 10299493_1407212372888741_1318245049_n.jpg
                                    [3] => 10318719_1407212379555407_451060715_n.jpg
                                )

But I want this array:

Array
(
[0] => /var/www/image/data/04.2014/C/AMANFTR105260005_001.jpg
[1] => /var/www/image/data/04.2014/C/Acki/10269990_1407183112891667_859040604_n.jpg
[2] => /var/www/image/data/04.2014/C/Acki/10287174_1407183106225001_914722369_n.jpg

...

[223] => /var/www/image/data/logo.gif
)

回答1:

<?php

$path = dirname(__FILE__);
$objects = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::SELF_FIRST
);
foreach ($objects as $file => $object) {
    $basename = $object->getBasename();
    if ($basename == '.' or $basename == '..') {
        continue;
    }
    if ($object->isDir()) {
        continue;
    }
    $fileData[] = $object->getPathname();
}
var_export($fileData);


回答2:

function dirToArray($dir, &$contents) {

    foreach (scandir($dir) as $node) {
        if ($node == '.' || $node == '.htaccess' || $node == '..' || $node == '.tmb' || $node == '.quarantine') continue;
        if (is_dir($dir . '/' . $node)) {
            dirToArray($dir . '/' . $node, $contents);
        } else {
            $contents[] = $dir . '/' . $node;
        }
    }

}

$contents = array();
dirToArray( '/var/www/image/data/', $contents );
print_r($contents);


回答3:

Just a few comments.

Same things as in OP for @dyachenko. You are checking for is_dir() while adding $node as file name to check, which means, this can never be true, since you are always getting file. Second: dirToArray() inside if statement is totally pointless, because it doesnt do anything until $content[] = $dir . '/' . $node is run, which will not be run if if statement is true.

So the correct code would be:

function dirToArray($dir, &$contents) {

    foreach (scandir($dir) as $node) {
        if ($node == '.' || $node == '.htaccess' || $node == '..' || $node == '.tmb' || $node == '.quarantine') continue;
        if (is_dir($dir)) {
          $contents[] = $dir . '/' . $node;
        }
        else {
          //error or what ever
        }
    }

}

$contents = array();
dirToArray( '/var/www/html/UusProjekt/intl', $contents );
print_r($contents);


标签: php spl scandir