问题描述
一旦用户上传了图片、pdf doc 和 ppt,我就会使用下面的代码在页面上显示文件大小并且它正在工作
$(document).ready(function(){
var fileName ;
$('.fileupload').change(function(e){
fileName = e.target.files[0].name; // get file name
var ext = fileName.split('.').pop();// check extention
var filesize= formatBytes(this.files[0].size);
$(this).closest(".upload_doc").find('.fileSizePreview').html(filesize);
});
function formatBytes(bytes,decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes','KB','MB','GB','TB','PB','EB','ZB','YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k,i)).toFixed(dm)) + ' ' + sizes[i];
}
});
现在我必须计算上传图片的大小,所以我尝试了下面的代码,但没有得到正确的输出。
$filename="xyz.png"; // or xyz.pdf
echo filesize($filename) . ' bytes';
echo format_size('/home/u886461223/domains/domain.com/public_html/test/example/assets/images/uploads/myprofile/70x70_1941629672.png');
function format_size($size) {
$sizes = array(" Bytes"," KB"," MB"," GB"," TB"," PB"," EB"," ZB"," YB");
if ($size == 0) { return('n/a'); } else {
return (round($size/pow(1024,($i = floor(log($size,1024)))),2) . $sizes[$i]); }
}
200Bytes,1.5KB,1MB
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)