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 post we will give you information about How to get file name, file size and sub directory from given directory. Hear we will give you detail about How to get file name, file size and sub directory from given directoryAnd how to use it also give you demo for it if it is necessary.

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:xampphtdocs directory.

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

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

    return $results;
}

$fileslist = getDirContents('C:xampphtdocsdemo');
echo "<pre>";
print_r($fileslist); 


Output :

Array
(
    [0] => Array
        (
            [path] => C:xampphtdocsdemofile1.jpg
            [size] => 23708
        )

    [1] => Array
        (
            [path] => C:xampphtdocsdemofile2.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. ?>
<?php
 print_r(glob("*.jpg"));
?>

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:xampphtdocsdemo');
print_r($files);

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

Try this..

Label :

PHP

File

How To

Apache

Web Development

Hope this code and post will helped you for implement How to get file name, file size and sub directory from given directory. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  2  =  3

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US