How to get file name, file size and sub directory from given directory

How to get file name, file size and sub directory from given directory

In this PHP tutorial, I will tell you how to read files and sub-directory from given directory with example.

Get file path and size

In this example, I have a folder/directory "demo" within C:\xampp\htdocs directory.

Now using below code, You can easily get all files and their size using PHP function.

  1. <?php
  2. function getDirContents($dir, &$results = array()){
  3. $files = scandir($dir);
  4. foreach($files as $key => $value){
  5. $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
  6. if(!is_dir($path)) {
  7. $results[] = ['path'=>$path,'size'=>filesize($path)];
  8. } else if($value != "." && $value != "..") {
  9. getDirContents($path, $results);
  10. $results[] = ['path'=>$path,'size'=>filesize($path)];
  11. }
  12. }
  13. return $results;
  14. }
  15. $fileslist = getDirContents('C:\xampp\htdocs\demo');
  16. echo "<pre>";
  17. print_r($fileslist);

Output :

Array
(
    [0] => Array
        (
            [path] => C:\xampp\htdocs\demo\file1.jpg
            [size] => 23708
        )

    [1] => Array
        (
            [path] => C:\xampp\htdocs\demo\file2.jpg
            [size] => 410510
        )
)

filesize function returns the size in bytes of specified file.

Return all sub directory without files

In this example, I will use PHP glob() method that returns an array of filenames or directories.

This is smarter way to find all files having specified pattern :

  1. <?php
  2. print_r(glob("*.jpg"));
  3. ?>

This will return all files having ".jpg" extension.

Array
(
	[0] => file1.jpg
	[1] => file2.jpg
)

Using glob method you can get all the directories and files.

$dirs = array_filter(glob('*'), 'is_dir');
print_r( $dirs);

This will return the directories without files.

$fileDir = array_filter(glob('*'));
print_r($fileDir);

This will return the directories with files.

Scan directory and files inside the specified path
$files=scandir('C:\xampp\htdocs\demo');
print_r($files);

This will return the array of files and directory from given path.

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort