PHP 文件名和文件大小输出


PHP File Name and File Size Output

如何使用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

我想为文件大小添加一个部分

可能使用相同的代码,但收集文件名信息,但在文件名旁边有一个新的

我不太关心文件大小输出,但更喜欢只输出 KB

谢谢,亚当

您应该在$file变量上使用函数filesize

然后你可以用这个小函数输出一个人类可读的大小:

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

您可以编写文件,然后使用 filesize 函数获取文件大小并使用文件名中的大小重命名文件。

http://php.net/manual/en/function.filesize.php

你可以把它添加到你的 foreach

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