如何获取foreach循环中文件的文件大小


how to get the filesize of files in a foreach loop

我试图回显目录中所有文件的文件大小,但它不回显文件大小,只回显文件名。这是我的代码:

$dir = "users/$UserName";
$files = scandir($dir);
sort($files);
echo '<table>';
foreach ($files as $file) {
if ($file != '.' && $file != '..') { 
?>
    <tr>
    <td><?php echo $file."<br />";
         ?>
    </td>
    <td>
        <?php echo filesize($file);   
        ?>
    </td>
    </tr>
<?php
}
}

我做错了什么?

AS m_poorUser说,如果你这样做,你可能需要将完整的文件路径传递给filesize()函数:

<?php echo filesize($dir."/".$file); ?>

scandir只返回文件名,而不返回文件的路径。

您应该在filesize($file) 中使用文件路径