我想要创建这样的脚本多年。我在下面尝试过,出于某种原因没有显示任何内容。其他人是否有任何类的例子。你认为什么是一种有效的展示方式。这基于用户输入值。
<?php
if($_GET['step'] == 2) {
$bytes = $_GET['bytes'];
echo $bytes;
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;
if (($bytes >= 0) && ($bytes < $kilobyte)) {
return $bytes;
}
if (($bytes >= $kilobyte) && ($bytes < $megabyte))
{
return $kb = round($bytes / $kilobyte);
}
if (($bytes >= $megabyte) && ($bytes < $gigabyte))
{
return $mb = round($bytes / $megabyte);
}
if (($bytes >= $gigabyte) && ($bytes < $terabyte))
{
return $gb = round($bytes / $gigabyte);
}
if ($bytes >= $terabyte)
{
return $tb = round($bytes / $terabyte);
}
else {
return $bytes;
}
$content .= "<h2>Details of Memory Size </h2>
<table>
<tr>
<th>Bytes</th>
<th>KB</th>
<th>MB</th>
<th>GB</th>
<th>TB</th
</tr>";
$content .= "<tr >
<td> $bytes</td>
<td>$kb </td>
<td>$mb</td>
<td>$gb</td>
<td>$tb</td>
</tr>
";
}
else {
$content = " <form action='index.php?step=2' method='post'>
<table>
<tr>
<th>Enter the number of kilobytes on unix drive</th>
<td><input type='text' name='bytes'/></td>
</tr>
<tr>
<td>
<input type='submit' />
</td>
</table>
</form>";
echo $bytes;
}
?>
答案 0 :(得分:0)
你想做这样的事吗?
<html>
<head>
<title>Byte scale calculator</title>
</head>
<body>
<?php
if ($_GET['step'] == 2) {
// Handle submissions
$bytes = $_GET['bytes'];
// Calculate KB
$kilobyte = 1024;
$kb = round($bytes / $kilobyte, 2);
// Calculate MB
$megabyte = $kilobyte * 1024;
$mb = round($bytes / $megabyte, 2);
// Calculate GB
$gigabyte = $megabyte * 1024;
$gb = round($bytes / $gigabyte, 2);
// Calculate TB
$terabyte = $gigabyte * 1024;
$tb = round($bytes / $terabyte, 2);
?>
<h2>Details of Memory Size </h2>
<table>
<tr>
<th>Bytes</th>
<th>KB</th>
<th>MB</th>
<th>GB</th>
<th>TB</th>
</tr>
<tr>
<td><?php echo $bytes; ?></td>
<td><?php echo $kb; ?></td>
<td><?php echo $mb; ?></td>
<td><?php echo $gb; ?></td>
<td><?php echo $tb; ?></td>
</tr>
</table>
<?php } ?>
<form action='' method='get'>
<input type='hidden' name='step' value='2' />
<!-- You had the form method set to post, but you were trying to fecth data from $_GET -->
<table>
<tr>
<th>Enter the number of kilobytes on unix drive</th>
<td><input type='text' name='bytes' /></td>
</tr>
<tr>
<td><input type='submit' /></td>
</tr>
</table>
</form>
</body>
</html>
有太多的变化要记录它们,所以如果有任何你不理解的东西,可以随意在底部发表评论。
答案 1 :(得分:-1)
我刚才在php docs注释中找到的一个函数,用于将字节转换为人类可读的支持SI和二进制前缀:
function size_readable($size, $max = null, $system = 'bi', $retstring = '%01.2f %s')
{
// Pick units
$systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB');
$systems['si']['size'] = 1000;
$systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$systems['bi']['size'] = 1024;
$sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];
// Max unit to display
$depth = count($sys['prefix']) - 1;
if ($max && false !== $d = array_search($max, $sys['prefix'])) {
$depth = $d;
}
// Loop
$i = 0;
while ($size >= $sys['size'] && $i < $depth) {
$size /= $sys['size'];
$i++;
}
return sprintf($retstring, $size, $sys['prefix'][$i]);
}