php中如何磁盘显示所有目录,PHP 返回一个目录的磁盘总大小

此博客介绍了如何使用PHP 6计算目录大小,包括递归遍历文件和子目录,并提供了一个非标准函数示例。同时,作者分享了如何在Windows系统上获取可用磁盘驱动列表。讨论了如何避免空文件和正确计算磁盘占用情况。
摘要由CSDN通过智能技术生成

用户评论:

khumlo at gmail dot com (2010-05-23 01:36:49)

PHP 6 has already come to the market but still there are no standard function that can help retrieve directory size as it has to calculate disk space such as disk_total_space. Although we can use system level call such as exec() and system(), it is too risky to enable these function. So we look for an alternate method so as to calculate directory size.

Sol: retrieving directory size using php

if (is_dir($dir_name)) {

if ($dh=opendir($dir_name)) {

while (($file=readdir($dh)) !==false) {

if($file!=”.”&&$file!=“..”){

if(is_file($dir_name.”/”.$file)){$dir_size+=filesize($dir_name.”/”.$file);

}/* check for any new directory inside this directory */if(is_dir($dir_name.”/”.$file)){$dir_size+=get_dir_size($dir_name.”/”.$file);

}

}

}

}

}closedir($dh);

return$dir_size;

}$dir_name=“directory name here”;/* 1048576 bytes == 1MB */$total_size=round((get_dir_size($dir_name) /1048576),2) ;

print“Directory $dir_name size:$total_size MB”;?>

Depending upon the size format you want to achieve, divide directory size by 1024 or 1024 * 1024 or 1024 * 1024 * 1024 for KB or MB or GB respectively.

cotact[at]covac-software[dot]com (2009-12-25 06:52:24)

Something that might go well with this function is the ability to list available disks. On Windows, here's the relevant code:

* Finds a list of disk drives on the server.

* @return array The array velues are the existing disks.

*/functionget_disks(){

if(php_uname('s')=='Windows NT'){// windows$disks=`fsutil fsinfo drives`;$disks=str_word_count($disks,1);

if($disks[0]!='Drives')return'';

unset($disks[0]);

foreach($disksas$key=>$disk)$disks[$key]=$disk.':\\';

return$disks;

}else{// unix$data=`mount`;$data=explode(' ',$data);$disks=array();

foreach($dataas$token)if(substr($token,0,5)=='/dev/')$disks[]=$token;

return$disks;

}

}?>

EXAMPLE OF USE:

EXAMPLE RESULT:

Array

(

[1] => A:\

[2] => C:\

[3] => D:\

[4] => E:\

[5] => F:\

[6] => G:\

[7] => H:\

[8] => I:\

[9] => M:\

[10] => X:\

[11] => Z:\

)

Warning: This also finds empty disk drives (eg; CD or SMD drives or the more common floppy drive).

Warning2: If you want to find space usage using the info from my function, prepend the disk function with the "@", eg:

$free=@disk_free_space('A:\\');

Viitala (2008-02-04 01:04:10)

Beware of empty files!

tularis at php dot net (2007-06-25 03:13:26)

For a non-looping way to add symbols to a number of bytes:

returnsprintf('%.2f '.$symbol[$exp], ($bytes/pow(1024,floor($exp))));

}

stierguy1 at msn dot com (2007-06-24 19:03:53)

function roundsize($size){

$i=0;

$iec = array("B", "Kb", "Mb", "Gb", "Tb");

while (($size/1024)>1) {

$size=$size/1024;

$i++;}

return(round($size,1)." ".$iec[$i]);}

nikolayku at tut dot by (2007-04-17 13:16:02)

Very simple function that convert bytes to kilobytes, megabytes ...

function ConvertBytes($number)

{

$len = strlen($number);

if($len < 4)

{

return sprintf("%d b", $number);

}

if($len >= 4 && $len <=6)

{

return sprintf("%0.2f Kb", $number/1024);

}

if($len >= 7 && $len <=9)

{

return sprintf("%0.2f Mb", $number/1024/1024);

}

return sprintf("%0.2f Gb", $number/1024/1024/1024);

}

Mat (2007-04-05 03:46:11)

JulieC:

I think you may have misunderstood - given a directory, this function tells you how big the disk paritition is that the directory exists on.

So disk_total_space("C:\Windows\") will tell you how big drive C is.

It is not suggesting that a directory is a disk partition.

JulieC (2007-01-30 19:11:21)

"filesystem or disk partition" does not equal "directory" for Windows. Thanks.

martijn at mo dot com (2007-01-13 14:41:20)

This works for me (on a UNIX server):

{$res= `du -sk$dir`;// Unix commandpreg_match('/\d+/',$res,$KB);// Parse result$MB=round($KB[0] /1024,1);// From kilobytes to megabytesreturn$MB;

}$dirSize=du('/path/to/dir/');?>

shalless at rubix dot net dot au (2003-07-16 07:36:54)

My first contribution. Trouble is the sum of the byte sizes of the files in your directories is not equal to the amount of disk space consumed, as andudi points out. A 1-byte file occupies 4096 bytes of disk space if the block size is 4096. Couldn't understand why andudi did $s["blksize"]*$s["blocks"]/8. Could only be because $s["blocks"] counts the number of 512-byte disk blocks not the number of $s["blksize"] blocks, so it may as well just be $s["blocks"]*512. Furthermore none of the dirspace suggestions allow for the fact that directories are also files and that they also consume disk space. The following code dskspace addresses all these issues and can also be used to return the disk space consumed by a single non-directory file. It will return much larger numbers than you would have been seeing with any of the other suggestions but I think they are much more realistic:

{$s=stat($dir);$space=$s["blocks"]*512;

if (is_dir($dir))

{$dh=opendir($dir);

while (($file=readdir($dh)) !==false)

if ($file!="."and$file!="..")$space+=dskspace($dir."/".$file);closedir($dh);

}

return$space;

}?>

andudi at gmx dot ch (2002-06-11 15:15:37)

To find the total size of a file/directory you have to differ two situations:

(on Linux/Unix based systems only!?)

you are interested:

1) in the total size of the files in the dir/subdirs

2) what place on the disk your dir/subdirs/files uses

- 1) and 2) normaly differs, depending on the size of the inodes

- mostly 2) is greater than 1) (in the order of any kB)

- filesize($file) gives 1)

- "du -ab $file" gives 2)

so you have to choose your situation!

on my server I have no rights to use "exec du" in the case of 2), so I use:

$s = stat($file);

$size = $s[11]*$s[12]/8);

whitch is counting the inodes [12] times the size of them in Bits [11]

hopes this helps to count the used disk place in a right way... :-)

Andreas Dick

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值