Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List content of directory (php)


I have a folder. I want to put every file in this folder into an array and afterwards I want to echo them all in an foreach loop.
What's the best way to do this?
Thanks!

like image 695
Burak Avatar asked Jan 16 '11 20:01

Burak


People also ask

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file. Consider the following directory structure −

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.


4 Answers

Scandir is what you're looking for http://php.net/manual/en/function.scandir.php

<?php
$dir    = '/tmp';
$files1 = scandir($dir);

print_r($files1);
?>

Or use combination of opendir and readdir http://php.net/manual/en/function.readdir.php

like image 63
dwich Avatar answered Sep 30 '22 14:09

dwich


Doesn't get much easier than this

http://ca3.php.net/manual/en/function.scandir.php

Don't forget to filter out hidden and parent directories (they start with a dot) on linux.

like image 43
Naatan Avatar answered Sep 30 '22 16:09

Naatan


An Alternative:

define('PATH', 'files/');

$filesArray = array();
$filesArray = glob(PATH . '*', GLOB_ONLYDIR);

This method allow you to specify/filter a by file type. E.G.,

define('PATH', 'files/');
define('FILE_TYPE', '.jpg');

$filesArray = array();
$filesArray = glob(PATH . '*' . FILE_TYPE, GLOB_ONLYDIR);

You can also get the FULL path name to the file by removing the parameter 'GLOB_ONLYDIR'

like image 37
Jarrod Avatar answered Sep 30 '22 15:09

Jarrod


This works for files and folders in subfolders too. Return list of folders and list of files with their path.



    $dir = __DIR__; //work only for this current dir
    function listFolderContent($dir,$path=''){
        $r = array();
        $list = scandir($dir);
        foreach ($list as $item) {
            if($item!='.' && $item!='..'){
                if(is_file($path.$item)){
                    $r['files'][] = $path.$item;
                }elseif(is_dir($path.$item)){
                    $r['folders'][] = $path.$item;
                    $sub = listFolderContent($path.$item,$path.$item.'/');
                    if(isset($sub['files']) && count($sub['files'])>0)
                        $r['files'] = isset ($r['files'])?array_merge ($r['files'], $sub['files']):$sub['files'];
                    if(isset($sub['folders']) && count($sub['folders'])>0)
                        $r['folders'] = array_merge ($r['folders'], $sub['folders']);
                }
            }
        }
        return $r;
    }
    $list = listFolderContent($dir);
    var_dump($list['files']);
    var_dump($list['folders']);
like image 37
DnD Avatar answered Sep 30 '22 14:09

DnD