2

can anyone help me to get each file size individually from a local directory ?.

 $files = scandir('soft');    
foreach($files as $file) {
    echo $file . "<br />";

}
Sahed
  • 21
  • 1
  • 6

3 Answers3

3

From here

 $files = scandir('soft');    
foreach($files as $file) {
 if (!in_array($file,array(".",".."))) 
  { 
    echo $file . "<br />";
    echo filesize('soft/'.$file) . ' bytes';
  }
}

Just need to keep in mind that scandir gets only the filenames in that dir, and not the relative path to it. that's why you need to use 'soft/'.$file and not $file

Paul Bele
  • 1,514
  • 1
  • 11
  • 12
  • 1
    `$file` -> `echo filesize('soft/'.$file) . ' bytes';` instead of `$filename` -> `echo filesize('soft/'.$filename) . ' bytes';` – Sean Apr 12 '14 at 04:42
  • sorry i was in a hurry, you are right, modified. Thanks ! – Paul Bele Apr 12 '14 at 04:42
  • thanks, but im having a issue, your code shows me 4096 bytes for every files and echoing two extra sizes which files are not exist. – Sahed Apr 12 '14 at 04:44
  • try now, it removes the '.' and '..' from the foreach loop – Paul Bele Apr 12 '14 at 04:47
  • @seblaze can u help me to get the file sizes in MB ? – Sahed Apr 12 '14 at 04:52
  • sure . The function you need to look is : number_format, based on this: http://stackoverflow.com/questions/5501427/php-filesize-mb-kb-conversion you can modify your code : number_format(filesize('tests/'.$file) / 1048576, 2); – Paul Bele Apr 12 '14 at 04:56
  • @seblaze thanks very much. can u help me with one more thing please? i need to see the each file last modified date.. is it possible? – Sahed Apr 12 '14 at 05:45
  • Yes it's possible. It's something like : date ("F d Y H:i:s.", filemtime('soft/'.$file)); From here : http://www.php.net/manual/en/function.filemtime.php – Paul Bele Apr 12 '14 at 06:03
1
<?
$files = scandir('.');
foreach($files as $file) {
  echo filesize($file) . " bytes<br>";
}
?>
Curtis W
  • 511
  • 4
  • 11
0

use filesize($filename) php function, it will give size in bytes

PravinS
  • 2,640
  • 3
  • 21
  • 25