i want get the sum of files size in folder by php
With DirectoryIterator and SplFileInfo
$totalSize = 0;
foreach (new DirectoryIterator('/path/to/dir') as $file) {
if ($file->isFile()) {
$totalSize += $file->getSize();
}
}
echo $totalSize;
and in case you need that including subfolders:
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('/path/to/dir')
);
$totalSize = 0;
foreach ($iterator as $file) {
$totalSize += $file->getSize();
}
echo $totalSize;
And you can run $totalSize
through the code we gave you to format 6000 to 6k for a more human readable output. You'd have to change all 1000s to 1024 though.
How to get directory size in PHP
The following are other solutions offered elsewhere:
If on a Windows Host:
<?
$f = 'f:/www/docs';
$obj = new COM ( 'scripting.filesystemobject' );
if ( is_object ( $obj ) )
{
$ref = $obj->getfolder ( $f );
echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
$obj = null;
}
else
{
echo 'can not create object';
}
?>
Else, if on a Linux Host:
<?
$f = './path/directory';
$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
echo 'Directory: ' . $f . ' => Size: ' . $size;
?>
How to get total size of multiple files php
If you just want to get the size of all uploaded files into your code, you can use array_sum()
, there is no need to use a loop.
$totalFileSize = array_sum($_FILES['upload']['size']);
this will give you the size in bytes of all files. then you can compare it to your max size :
$maxFileSize = 100 * 1024 * 1024 /* 100MB */;
if ($totalFileSize > $maxFileSize) {
echo 'Your files exceed the limit of 100MB capacity';
}
based on your code, here is the full code with array_sum
to get the total file size
<?php
if (isset($_POST['submit'])) {
if (count($_FILES['upload']['name']) > 0) {
// compute the total size of the uploaded files
$totalFileSize = array_sum($_FILES['upload']['size']);
echo 'upload size : ' . $totalFileSize . ' bytes';
$maxFileSize = 100 * 1024 * 1024;
// check if the upload size is less than the max allowed
if ($totalFileSize > $maxFileSize) {
echo 'Your files exceed the limit of 100MB capacity';
} else {
// upload size is OK, process files
for ($i = 0; $i < count($_FILES['upload']['name']); $i++) {
$fileName = $_FILES['upload']['name'][$i];
$fileExt = strtolower(pathinfo($_FILES['upload']['name'][$i], PATHINFO_EXTENSION));
if (empty($fileName)) {
echo 'Please select photos to upload!';
} else {
if (!in_array($fileExt, ['jpg', 'jpeg', 'png', 'gif', 'bmp'])) {
echo 'Only photos, videos and audios allowed. If you have one or more files that is not in our <a href="#">supported extensions</a> directory, please remove it!';
} else {
echo "Uploaded";
}
}
}
}
}
}
?>
<form action="" enctype="multipart/form-data" method="post">
<div>
<label for='upload'>Add Attachments:</label>
<input id='upload' name="upload[]" type="file" multiple="multiple"/>
</div>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
Calculate the weight of a directory (folder) in php
Your test for directory is incorrect here:
if (is_dir($file)) // This test is missing the directory component
{
$cont += Fsize($dir."/".$file);
}
else
Try:
if (is_dir("$dir/$file")) // This test adds the directory path
{
$cont += Fsize($dir."/".$file);
}
else
PHP offers a number of iterators that can simplify operations like this:
$path = "path/to/folder";
$Directory = new RecursiveDirectoryIterator($path);
$Iterator = new RecursiveIteratorIterator($Directory);
$Iterator->setFlags(FilesystemIterator::SKIP_DOTS);
$totalFilesize = 0;
foreach($Iterator as $file){
if ($file->isFile()) {
$totalFilesize += $file->getSize();
}
}
echo "Total: $totalFilesize";
Set Max Size to folder for users file manager php mysql
Which type of files will it be? I would be a very wary of allowing users to upload arbitrary files to my web server. If they upload a .php
file they would be able to run anything as the user running the webserver.
In fact, I would keep them in a database, and have fields to create the meta data instead. this way, you can compress and save the files, and store the size and filename as meta data. If you are using if for reference, etc, which does not need quick access, I would recommend this.
Alternatively, as this this linux.SE answer states, you could create a mounted folder for each user, and have the filesystem sort out everything regarding the quota. I would probably also look into chroot
ing the folders in some way. This would (in theory) also allow you to give them sftp
/ssh
access to the files.
I Would also look into doing everything in an environment similar to your production server. vagrant and docker spring to mind.
Alternatively, if all file uploads are handled by php, you could save the used file space and keep a running total, making sure in your php sript that they are nmot exceeding their quota.
Restrict Uploading if the Folder size is more than 10 GB
The following code will calculate the total size of all files in a folder therefore giving you total folder size
<?php
$path = "gal";
echo "Folder $path = ".filesize_r($path)." bytes";
function filesize_r($path){
if(!file_exists($path)) return 0;
if(is_file($path)) return filesize($path);
$ret = 0;
foreach(glob($path."/*") as $fn)
$ret += filesize_r($fn);
return $ret;
}
?>
Then you can build an if statement before the upload that will check the file size and if it is larger than 10gb, alert the user that the upload cannot be processed.
EDIT:
The IF-statement will look something like this
$filesize = filesize_r($path)
if $filesize > 10 000 000 000 {
echo "Upload cannot be processed";
} else {
uploadFile()
}
Related Topics
Woocommerce New Order Action - Get Order Information
Mongodb Aggregate Query Using PHP Driver
PHP Xpath - Find Element with a Value and Also Get Elements Before and After Element
How to Change the Name of an Element in Dom
Where to Use MySQL_Real_Escape_String to Prevent SQL Injection
How to Add More Custom Field in Linked Product of Woocommerce
How to Keep Leading Zeros in PHP Integer
Simplexml: Append One Tree to Another
How to Grab All Variables in a Post (Php)
No Mail Received in Inbox with Xampp 1.8.0, Mercurymail and Mail()
Can Mongodb and Its Drivers Preserve the Ordering of Document Elements
Yii2 Links Between Frontend and Backend (Advanced Template)
Variable Variables in PHP - What Is Their Purpose