2012-04-29 61 views
-1

如何在使用PHP的文件名旁邊添加文件大小?PHP文件名和文件大小輸出

目前正在使用此:

<?php 
# Configuration 
$show_path = 0; # Show local path. 
$show_dotdirs = 1; # Show '.' and '..'. 

$path = substr($_SERVER['SCRIPT_FILENAME'], 0, 
    strrpos($_SERVER['SCRIPT_FILENAME'], '/') + 1); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link rel="stylesheet" type="text/css" href="style.css"/> 
</head> 

<body> 
<div id="menu"> 
    <div id="menualign"><a href="index.php"><img src="images/home.png" onmouseover="src='images/homed.png'" onmouseout="src='images/home.png'"/></a><img src="images/moviesa.png"/><a href="tvguide.php"><img src="images/tv.png" onmouseover="src='images/tvd.png'" onmouseout="src='images/tv.png'"/></a></div> 
</div> 
<div id="container"> 
    <div id="fleft"> 
    <table cellspacing="1"> 
     <tr> 
     <th><?php if ($show_path == 1) { echo $path; } else { echo 'Current Movies'; } ?></th> 
     </tr> 
     <tr> 
     <td align="left"><?php 
$dirs = array(); 
$files = array(); 

$dir = dir('../Movies/'); 
while ($entry = $dir->read()) { 
    if (($entry != '.') and (substr($entry, -4) != '.php')) { 
     if (is_dir($entry)) { 
      if (($entry != '..') or $show_dotdirs){ 
       $dirs[] = $entry; 
      } 
     } else { 
      $files[] = $entry; 
     } 
    } 
} 
$dir->close(); 

sort($dirs); 
foreach ($dirs as $dir) { 
    printf('<strong>&lt;</strong> <a href="%s">%s</a> <strong>&gt;</strong><br />' . "\n", $dir, $dir); 
} 

sort($files); 
foreach ($files as $file) { 
    printf('<hr />%s<br />' . "\n", $file, $file); 
} 
?></td> 
     </tr> 
    </table> 
    </div> 
    <div id="fright">TEST 
    </div> 
</div> 
</body> 
</html> 

,其輸出爲這樣:http://downunderpctech.com/output.png

我想補充一節作品尺寸

可能使用相同的代碼,但收集文件大小信息作爲文件名,但在一個新的旁邊它

我不太在意Filesize輸出,但只會選擇ou tput的KB

謝謝,亞當

回答

3

你應該使用功能filesize$file變量。

然後你就可以輸出人類可讀的大小與this little function

function file_size($size) 
{ 
    $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); 
    return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes'; 
} 

您的# Configuration之前定義的函數,然後把它在你的foreach:

echo file_size(filesize('../Movies/'.$file)); 
+0

我如何在我的foreach中使用此函數? – 2012-04-29 09:16:26

+0

我已經更新了我的答案 – j0k 2012-04-29 10:21:38

+0

謝謝,最終以我想要的方式工作 – 2012-04-29 12:11:31

0

您可以添加到您的foreach

echo '<span>'.round(filesize ('../Movies/'.$file)/1024, 1).' Kb</span>'; 
+0

行,所以我也做這個: sort($ files); 的foreach($文件作爲$文件){ \t如果($ tick1!= 0) \t { \t \t回聲 「


」; \t} printf('%s
'。「\ n」,$ file,$ file); echo round(文件大小($文件)/ 1024,1)。' KB
'; \t $ tick1 = $ tick1 + 1; } 而我從每個文件得到的所有內容都是0KB ... – 2012-04-29 09:36:07

+0

@AdamCoulson,固定 – Adi 2012-04-29 10:46:19