服务器端代码(在控制器):
以下功能从Drupal的由meustrus作者在他的stack answer采取和我在这里作为例子。 开始的post_max_size
// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
$max_size = parse_size(ini_get('post_max_size'));
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
//Get max upload file size limit...
$file_upload_max_size = $max_size;
公共函数来分析大小
public function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
设置紧凑的发送 'file_upload_max_size' 价值刀片
return view('YOURBLADEPATH',compact('file_upload_max_size'));
JS验证(在刀片):
<script type="text/javascript">
document.forms[0].addEventListener('submit', function(evt) {
var file = document.getElementById('file').files[0];
if(file && file.size < '{$file_upload_max_size}') { // 10 MB (this size is in bytes)
//Submit form
} else {
//Prevent default and display error
evt.preventDefault();
}
}, false);
http://stackoverflow.com /问题/ 2840755 /如何到确定最最大文件上传限制功能于PHP –
看看我的答案 –