遍历文件夹下的所有文件,大理石构件来图加工

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

<?php

/**

* 遍历当前文件夹展示所有的文件和目录

*/

function dirList($dir_path = '') {

    if(is_dir($dir_path)) {

        $dirs = opendir($dir_path);

        if($dirs) {

            while(($file = readdir($dirs)) !== false) {

                if($file !== '.' && $file !== '..') {

                    if(is_dir($file)) {

                        echo $dir_path . '/' . $file . '<br>';

                        dirList($dir_path . '/' . $file);

                    } else {

                        echo $dir_path . '/' . $file . '<br>';

                    }

                }

            }

            closedir($dirs);

        }

    } else {

        echo '目录不存在!';

    }

}

dirList('/var/www/html/php-demo');

function dir_list($dir) {

    if(!is_dir($dir)) return false;

    $dir_list = array();

    $opendir = opendir($dir);

    if($opendir) {

        while(($file = readdir($opendir)) !== false) {

            if($file !== '.' && $file !== '..') {

                $tem = $dir . '/' . $file;

                if(is_dir($tem)) {

                    $dir_list[$tem . '/'] = $file . '/';

                    dir_list($tem);

                } else {

                    $dir_list[] = $file;

                }

            }

        }

        closedir($opendir);

        return $dir_list;

    }

}

$dir = dir_list('/var/www/html/php-demo');

var_dump($dir);

02-13 05:22