Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

spl - php get full path of file in a folder

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
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<?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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...