get Directory size Laravel – How to get Folder or Directory size in Laravel

In this post, we wiull show you how to get Directory size Laravel, we have a tendency to area unit getting to tell you ways to urge directory size in PHP Laravel.

As all is aware of, PHP has intrinsic operate to urge file size victimisation filesize() technique however ever you’re thinking that a way to get directory size in PHP or a way to browse directory files and sub directory among directory?

A folder could have several files and have subfolder that contains several files too.

So here you’ll knowledge to ingeminate over folders and their files.

PHP operate can come back size in bytes thus 1st we have a tendency to write a operate to convert bytes to KB, Megabyte, G and computer memory unit etc.

PHP Function to format size for get Directory size Laravel

function format_Size($set_bytes)
{ 
	$set_kb = 1024;
	$set_mb = $set_kb * 1024;
	$set_gb = $set_mb * 1024;
	$set_tb = $set_gb * 1024;
	if (($set_bytes >= 0) && ($set_bytes < $set_kb)) 
	{
		return $set_bytes . ' B';
	} 
	elseif (($set_bytes >= $set_kb) && ($set_bytes < $set_mb)) 
	{
		return ceil($set_bytes / $kb) . ' KB';
	} 
	elseif (($set_bytes >= $set_mb) && ($set_bytes < $set_gb)) 
	{
		return ceil($set_bytes / $set_mb) . ' MB';
	} 
	elseif (($set_bytes >= $set_gb) && ($set_bytes < $set_tb)) 
	{
		return ceil($set_bytes / $set_gb) . ' GB';
	} 
	elseif ($set_bytes >= $set_tb) 
	{
		return ceil($set_bytes / $set_tb) . ' TB';
	} else {
		return $set_bytes . ' Bytes';
	}
}

Now we will write a function to scan directory and return size of directory.

<h2>PHP Function to return directory size for get Directory size Laravel</h2>


function folder_Size($set_dir)
{
	$set_total_size = 0;
	$set_count = 0;
	$set_dir_array = scandir($set_dir);
	foreach($set_dir_array as $key=>$set_filename)
	{
		if($set_filename!=".." && $set_filename!=".")
		{
		    if(is_dir($set_dir."/".$set_filename))
		    {
			    $new_foldersize = folder_Size($set_dir."/".$set_filename);
			    $set_total_size = $set_total_size+ $new_foldersize;
			}
			else if(is_file($set_dir."/".$set_filename))
			{
			    $set_total_size = $set_total_size + filesize($set_dir."/".$set_filename);
			    $set_count++;
			}
	    }
	}
	return $set_total_size;
}

Define Folder and pass it to function to get size for get Directory size Laravel

// add folder name hear
$set_folder_path = "Your_folder_path";
echo format_Size(folder_Size($set_folder_path));

Laravel Load More Data on Page Scroll AND Laravel Custom Helpers AND how to laravel latest version AND Laravel login using google