Search code examples
phpfilesize

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?


Solution

  • 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); ?>