Jack Maessen Jack Maessen - 7 months ago 14
PHP Question

how to get the filesize of files in a foreach loop

I am trying to echo the filesize of all files in a directory but it doesn't echo the filesize, only the filename. This is my code:

$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

}
}


what i am doing wrong?

M.M M.M
Answer

AS m_poorUser said you need to pass the full file path to the filesize() function, maybe if you do this:

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