Server-side code (in the controller):
The function below is taken from drupal by meustrus in his answer, and I gave an example here. Start with 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 for parsing 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); } }
Set compact to send the value of 'file_upload_max_size' in click
return view('YOURBLADEPATH',compact('file_upload_max_size'));
JS Check (In Blade):
<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);
AddWeb Solution Pvt Ltd
source share