douchuilai2355 2014-07-10 15:44
浏览 68
已采纳

PHP MySQL - 需要显示文件列表及其文件大小,格式化。 Redeclare功能错误

I'm stuck on something that's probably simple for more experienced PHP folks. I'm getting a "cannot redeclare function" error when I try to format the file size of a list of files I'm querying a mysql database for.

From what I can tell, I shouldn't put the function inside the while statement, but if I don't, the $bytes variable won't change for each file that's called from the database. Can someone explain how I can make this work?

Here's the code:

while ($docRows = mysql_fetch_array($docsQuery)) {

 $docID = $docRows['ID'];
 $docName = htmlspecialchars($docRows['docName']);
 $docDescription = htmlspecialchars($docRows['LEFT(docDescription, 50)']);
 $docPath = $docRows['filePath'];
 $origName = $docRows['origName'];
 $bytes = filesize($_SERVER['DOCUMENT_ROOT']."/documents/".$origName."");

 function bytesToSize($bytes, $precision = 2) {  
$kilobyte = 1024;
$megabyte = $kilobyte * 1024;
$gigabyte = $megabyte * 1024;
$terabyte = $gigabyte * 1024;

if (($bytes >= 0) && ($bytes < $kilobyte)) {
    return $bytes . ' B';

} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
    return round($bytes / $kilobyte, $precision) . ' KB';

} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
    return round($bytes / $megabyte, $precision) . ' MB';

} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
    return round($bytes / $gigabyte, $precision) . ' GB';

} elseif ($bytes >= $terabyte) {
    return round($bytes / $terabyte, $precision) . ' TB';
} else {
    return $bytes . ' B';
}
}

echo ('<tr>
    <td><a href="'.$docPath.'">'.$docName.'</a> ('.bytesToSize($bytes).')</td>
    <td>');

    if (!empty($docDescription)) {
    echo(''.$docDescription.'...');
    }
 else {
    // don't show description if there isn't one
    }

echo ('</td>
</tr>
');

} // end while
  • 写回答

2条回答 默认 最新

  • doufu8887 2014-07-10 16:45
    关注

    You need to define your function then call it, if you define a function more than once PHP will throw an error, that is why you should not define your function in a loop.

    regarding echo it does not require parenthesis, please take a look at the below code:

    function bytesToSize($bytes, $precision = 2) {  
        $kilobyte = 1024;
        $megabyte = $kilobyte * 1024;
        $gigabyte = $megabyte * 1024;
        $terabyte = $gigabyte * 1024;
    
        if (($bytes >= 0) && ($bytes < $kilobyte)) {
            return $bytes . ' B';
    
        } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
            return round($bytes / $kilobyte, $precision) . ' KB';
    
        } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
            return round($bytes / $megabyte, $precision) . ' MB';
    
        } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
            return round($bytes / $gigabyte, $precision) . ' GB';
    
        } elseif ($bytes >= $terabyte) {
            return round($bytes / $terabyte, $precision) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
    
    while ($docRows = mysql_fetch_array($docsQuery)) {
         $docID = $docRows['ID'];
         $docName = htmlspecialchars($docRows['docName']);
         $docDescription = htmlspecialchars($docRows['LEFT(docDescription, 50)']);
         $docPath = $docRows['filePath'];
         $origName = $docRows['origName'];
         $bytes = filesize($_SERVER['DOCUMENT_ROOT']."/documents/".$origName."");
    
        echo '<tr>
            <td><a href="'.$docPath.'">'.$docName.'</a> ('.bytesToSize($bytes).')</td>
            <td>';
    
        if (!empty($docDescription)) {
            echo(''.$docDescription.'...');
        }
         else {
        // don't show description if there isn't one
        }
    
        echo '</td>
            </tr>
        ';
    
    } // end while
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止