dspym82000 2012-04-29 05:39 采纳率: 100%
浏览 27
已采纳

PHP文件名和文件大小输出

How can i add a file size beside my file names using PHP???

Currently Using This:

<?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 />' . "
", $dir, $dir);
}

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

Which outputs as this: http://downunderpctech.com/output.png

I Would like to Add a section for Filesize

Probably using the same code but to collect filesize info as the Filename but in a new beside it

I am not too concerned about the Filesize output, but would prefer only to output KB

Thanks, Adam

  • 写回答

3条回答 默认 最新

  • duanboniao5903 2012-04-29 05:58
    关注

    You should use the function filesize on your $file variable.

    And then you can output a human readable size with 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';
    }
    

    You define the function before the # Configuration and then you call it in your foreach:

    echo file_size(filesize('../Movies/'.$file));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效