The dir_size function provided by "marting.dc AT gmail.com" works great, except the $mas variable is not initialized. Add:
$mas = 0;
before the while() loop.
stat
(PHP 4, PHP 5, PECL maxdb:7.5.00.24-7.6.00.34)
stat — ファイルに関する情報を取得する
説明
array stat ( string $filename )filenameで指定されたファイルに ついての統計情報を取得します。 filename がシンボリックリンクの場合、 シンボリックリンクではなくファイルの実体の統計情報が返されます。 lstat() はシンボリックリンクの統計情報を返すという違いを除いて stat() と等価です。
stat() はエラーの場合 FALSE を返し、かつ warning を発行します。
ファイルの統計情報を、以下の要素を持つ配列として返します。 ゼロベース(添え字は数値)の配列に加えて、 各パラメータ毎の連想配列としてもアクセスできます。 これは PHP 4.0.6 以上で使用できます。
表 92. stat() と fstat() の結果のフォーマット
| 数値 | 連想 (PHP 4.0.6 以上) | 説明 |
|---|---|---|
| 0 | dev | デバイス番号 |
| 1 | ino | inode 番号 |
| 2 | mode | inode プロテクトモード |
| 3 | nlink | リンク数 |
| 4 | uid | 所有者のユーザ ID |
| 5 | gid | 所有者のグループ ID |
| 6 | rdev | inode デバイス の場合、デバイスの種類(*) |
| 7 | size | バイト単位のサイズ |
| 8 | atime | 最終アクセス時間 (Unix タイムスタンプ) |
| 9 | mtime | 最終修正時間 (Unix タイムスタンプ) |
| 10 | ctime | 最終 inode 変更時間 (Unix タイムスタンプ) |
| 11 | blksize | ファイル IO のブロックサイズ(*) |
| 12 | blocks | ブロックの確保数 |
* st_blksize タイプをサポートするシステムでのみ有効です。 その他のシステム(例えば Windows)では -1 を返します。
注意: この関数の結果は キャッシュされます。詳細は、clearstatcache() を参照してください。
ティップ
PHP 5.0.0 以降、この関数は、 何らかの URL ラッパーと組合せて使用することができます。 どのラッパーが stat() ファミリーをサポートしているか のリストについては、付録 O. サポートされるプロトコル/ラッパー を参照してください。
lstat()、 fstat()、 filemtime() および filegroup() も参照ください。
stat
JulieC
31-Jan-2007 12:21
31-Jan-2007 12:21
piranha-php dot net at thoughtcrime dot us
17-Jul-2006 09:04
17-Jul-2006 09:04
stat() returns a file's _status_, not its _statistics_. "Statistics" implies information interpreted from the data of several files, not concrete meaning from a single file. Both Linux and POSIX manual pages for stat() list the name as "stat - get file status," and do not mention the word "statistic" anywhere.
hugues dot larrive at gmail dot com
09-Jun-2006 05:49
09-Jun-2006 05:49
salisbm at hotmail dot com said :
(...)to see if the file is a directory, after calling fstat, I do:
if ($fstats[mode] & 040000)
... this must be a directory
Then I say no no no no... it can be a directory or a named pipe, or a block spécial ...
The good code for this thing is :
<?
if(($fstat['mode'] & 0170000) == 040000) echo "Be sure it is a directory !";
?>
Sorry for very ugly english ;)
@+
17-May-2006 02:10
Re note posted by "admin at smitelli dot com"
I'm not sure how that can work all year round since you have to modify both opposing inside and outside DST based on the actual files themselves, as well as the current DST setting for the system.
e.g. using filemtime, same thing for stat.
<?php
$mtime = filemtime($file);
if (date('I') == 1) {
// Win DST is enabled, adjust standard time
// files back to 'real' file UTC.
if (date('I', $mtime) == 0) {
$mtime -= 3600;
}
} else {
// Win DST is disabled, adjust daylight time
// files forward to 'real' file UTC.
if (date('I', $mtime) == 1) {
$mtime += 3600;
}
}
echo gmdate('Y-m-d H:i:s', $mtime);
?>
Just another example of why 'not' to use windows in a server room.
marting.dc AT gmail.com
29-Jan-2006 10:08
29-Jan-2006 10:08
If you want to know a directory size, this function will help you:
<?
function dir_size($dir)
{
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) {
$mas += filesize($dir.'/'.$file);
} else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') {
$mas += dir_size($dir.'/'.$file);
}
}
return $mas;
}
echo dir_size('DIRECTORIO').' Bytes';
?>
admin at smitelli dot com
03-Nov-2005 04:27
03-Nov-2005 04:27
There's an important (yet little-known) problem with file dates on Windows and Daylight Savings. This affects the 'atime' and 'mtime' elements returned by stat(), and it also affects other filesystem-related functions such as fileatime() and filemtime().
During the winter months (when Daylight Savings isn't in effect), Windows will report a certain timestamp for a given file. However, when summer comes and Daylight Savings starts, Windows will report a DIFFERENT timestamp! Even if the file hasn't been altered at all, Windows will shift every timestamp it reads forward one full hour during Daylight Savings.
This all stems from the fact that M$ decided to use a hackneyed method of tracking file dates to make sure there are no ambiguous times during the "repeated hour" when DST ends in October, maintain compatibility with older FAT partitions, etc. An excellent description of what/why this is can be found at http://www.codeproject.com/datetime/dstbugs.asp
This is noteworthy because *nix platforms don't have this problem. This could introduce some hard-to-track bugs if you're trying to move scripts that track file timestamps between platforms.
I spent a fair amount of time trying to debug one of my own scripts that was suffering from this problem. I was storing file modification times in a MySQL table, then using that information to see which files had been altered since the last run of the script. After each Daylight Savings change, every single file the script saw was considered "changed" since the last run, since all the timestamps were off by +/- 3600 seconds.
This one-liner is probably one of the most incorrect fixes that could ever be devised, but it's worked flawlessly in production-grade environments... Assuming $file_date is a Unix timestamp you've just read from a file:
<?php
if (date('I') == 1) $file_date -= 3600;
?>
That will ensure that the timestamp you're working with is always consistently reported, regardless of whether the machine is in Daylight Savings or not.
com dot gmail at algofoogle
22-Jul-2005 10:06
22-Jul-2005 10:06
Re note posted by "salisbm at hotmail dot com":
S_IFDIR is not a single-bit flag. It is a constant that relies on the "S_IFMT" bitmask. This bitmask should be applied to the "mode" parameter before comparing with any of the other "S_IF..." constants, as indicated by stat.h:
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
That is, this approach is incorrect:
<?php
define('S_IFDIR',040000);
if ($mode & S_IFDIR)
{
/*
incorrect!
format could be S_IFDIR, but also
S_IFBLK, S_IFSOCK, or S_IFWHT.
*/
}
?>
...and should instead be:
<?php
define('S_IFMT',0170000);
define('S_IFDIR',040000);
if (S_IFDIR == ($mode & S_IFMT)) { /* ... */ }
?>
As pointed out by "svend at svendtofte dot com", however, there is also the "is_dir" function for this purpose, along with "is_file" and "is_link" to cover the most common format types...
mpb dot mail at gmail dot com
18-Jul-2005 07:10
18-Jul-2005 07:10
If you are working with files larger than 2GB (and PHP's integer type is only 32 bits on your system) then you can try the following to get floating point sizes:
On FreeBSD:
$size = (float) exec ('stat -f %z '. escapeshellarg ($path));
On Linux:
$size = (float) exec ('stat -c %s '. escapeshellarg ($path));
(The other example that uses "ls" and "awk" does not properly escape the filename, but should work otherwise.)
mao at nospam dot com
07-Jun-2005 09:53
07-Jun-2005 09:53
If you have ftp (and the related sftp) protocols disabled on your remote server, it can be hard figuring out how to 'stat' a remote file. The following works for me:
<?php
$conn = ssh2_connect($host, 22);
ssh2_auth_password($conn, $user, $password);
$stream = ssh2_exec($conn, "stat $fileName > $remotedest");
ssh2_scp_recv($conn, $remotedest, $localdest);
$farray = file($localdest);
print_r($farray);
?>
guillermo martinez
31-Jan-2005 03:24
31-Jan-2005 03:24
stat() and SELinux,
You can have troubles to use the stat() function if the SELinux is enabled, so check the SELinux documentation or turn it off.
11-Nov-2004 12:41
If the 2GB limit is driving you crazy, you can use this complete hack. use in place of filesize()
function file_size($file) {
$size = filesize($file);
if ( $size == 0)
$size = exec("ls -l $file | awk '{print $5}'");
return $size;
}
svend at svendtofte dot com
10-Oct-2004 08:31
10-Oct-2004 08:31
To the note of how you can figure out if a file is a folder or not, there is also the handy "is_dir" function.
salisbm at hotmail dot com
12-Aug-2003 09:21
12-Aug-2003 09:21
I was curious how I could tell if a file was a directory... so I found on http://www.hmug.org/man/2/stat.html the following information about the mode bits:
#define S_IFMT 0170000 /* type of file */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
#define S_IFWHT 0160000 /* whiteout */
#define S_ISUID 0004000 /* set user id on execution */
#define S_ISGID 0002000 /* set group id on execution */
#define S_ISVTX 0001000 /* save swapped text even after use */
#define S_IRUSR 0000400 /* read permission, owner */
#define S_IWUSR 0000200 /* write permission, owner */
#define S_IXUSR 0000100 /* execute/search permission, owner */
Note that these numbers are in octal format. Then, to check to see if the file is a directory, after calling fstat, I do:
if ($fstats[mode] & 040000)
... this must be a directory
ian at eiloart dot com
24-Jul-1999 12:52
24-Jul-1999 12:52
Here's what the UNIX man page on stat has to say about the difference between a file change and a file modification:
st_mtime Time when data was last modified. Changed by the following functions: creat(), mknod(), pipe(), utime(), and write(2).
st_ctime Time when file status was last changed. Changed by the following functions: chmod(), chown(), creat(), link(2), mknod(), pipe(), unlink(2), utime(), and write().
So a modification is a change in the data, whereas a change also happens if you modify file permissions and so on.