2014-04-12 24 views
1

中分別獲取每個文件的大小,任何人都可以幫助我從本地目錄單獨獲取每個文件大小?如何從目錄PHP

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

} 

回答

2

here

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

只需要記住,scandir僅獲取文件名在該目錄,而不是它的相對路徑。這就是爲什麼你需要使用'soft/'.$file而不是$file

+1

'$ file' - >'echo filesize('soft /'.$ file)。 'bytes';'而不是'$ filename' - >'echo filesize('soft /'.$ filename)。 'bytes';' – Sean

+0

對不起,我很着急,你說得對,修改過。謝謝 ! –

+0

謝謝,但即時通訊有問題,您的代碼顯示每個文件4096個字節,並回顯兩個額外的大小哪些文件不存在。 – Sahed

1
<? 
$files = scandir('.'); 
foreach($files as $file) { 
    echo filesize($file) . " bytes<br>"; 
} 
?> 
0

使用filesize($filename) PHP函數,它會給字節大小