PHP, JavaScript로 byte를 Kb, Mb, Gb, Tb로 변환 > php

본문 바로가기

php

PHP, JavaScript로 byte를 Kb, Mb, Gb, Tb로 변환

작성일 16-05-18 15:47

페이지 정보

작성자서방님 조회 577회 댓글 0건

본문

<?
/**
 * byte를 용량에 따라 b, kb, mb, gb, tb로 계산하여 리턴함 (PHP)
 *
 * @param int bytes
 * @return String
 */
function byteConvert($bytes) {
$s = array('B', 'Kb', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes)/log(1024));

return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
}
?>

<script type="text/javascript">
<!--
/**
 * byte를 용량에 따라 b, kb, mb, gb, tb로 계산하여 리턴함 (JavaScript)
 *
 * @param int bytes
 * @return String
 */
function byteConvertor(bytes) {
bytes = parseInt(bytes);
var s = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
var e = Math.floor(Math.log(bytes)/Math.log(1024));
if(e == "-Infinity") return "0 "+s[0];
else return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}
//-->
</script>

자바스크립트에서는 if(e == "-Infinity")부분을 추가 했습니다.
0바이트가 넘어갈때 나는 오류를 처리하기 위함입니다.


댓글목록

등록된 댓글이 없습니다.

게시물 검색
Copyright © 서방님.kr All rights reserved.
PC 버전으로 보기