如何显示文件的大小


How can I display the size of a file?

现在我可以列出一个目录中的所有文件。我正在使用这个代码,它工作得很好:

 <?php
    if ($handle = opendir('./uploaded')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                echo '<div class="col-md-3"><div class="panel panel-default"><!-- Default panel contents --><div class="panel-heading">'.$entry.'</div><div class="panel-body">'.$entry.'</div><div class="panel-footer"><a href="./uploaded/'.$entry.'">View File</a></div></div></div>';
            }
        }
    closedir($handle);
    }
?>

现在,我想在while部分显示有关该文件的所有信息。我见过有人在C语言和其他语言中使用类似的技术来实现这一点。

尝试fstat了解更多详细信息

说明¶

array fstat ( resource $handle )

收集由文件指针句柄打开的文件的统计信息。此函数类似于stat()函数,不同之处在于它对打开的文件指针而不是文件名进行操作。

<?php
// open a file
$fp = fopen("/etc/passwd", "r");
// gather statistics
$fstat = fstat($fp);
// close the file
fclose($fp);
// print only the associative part
print_r(array_slice($fstat, 13));
?>

输出:

Array
(
    [dev] => 771
    [ino] => 488704
    [mode] => 33188
    [nlink] => 1
    [uid] => 0
    [gid] => 0
    [rdev] => 0
    [size] => 1114
    [atime] => 1061067181
    [mtime] => 1056136526
    [ctime] => 1056136526
    [blksize] => 4096
    [blocks] => 8
)

这样尝试。。

<?php
     if ($handle = opendir('./uploaded')) {
       while (false !== ($entry = readdir($handle))) {
         if ($entry != "." && $entry != "..") {
          echo '<div class="col-md-3"><div class="panel panel-default"><!-- Default panel contents --><div class="panel-heading">'.$entry.'</div><div class="panel-body">'.$entry.'</div><div class="panel-footer"><a href="./uploaded/'.$entry.'">View File</a></div></div></div>';
          echo $entry . ': ' . filesize($entry) . ' bytes'; // Gets file size e.g. xyz.txt: 1024 bytes
         }
       }
       closedir($handle);
     }
?>