Component

Module

Validate Max File Size For Upload (Image)

Set the file upload size limit in the setting.

Directory

File Folder Link
BusinessProfileController.php \\SYNAS\Allan\DOCUMENTATION\Component\Validate Max File Size For Upload (Image)\rckuc\wolf\app\controllers

 

Step 1

Insert SQL Record into wolf_module_setting:

INSERT INTO `wolf_module_setting` (`id`, `module`, `name`, `value_type`, `value`, `created_on`, `updated_on`, `created_by_id`, `updated_by_id`) VALUES (NULL, 'page', 'Max Img Size Upload', 'number', '20', NOW(), NULL, NULL, NULL);
INSERT INTO `wolf_module_setting` (`id`, `module`, `name`, `value_type`, `value`, `created_on`, `updated_on`, `created_by_id`, `updated_by_id`) VALUES (NULL, 'page', 'Max Img Size Upload', 'number', '20', NOW(), NULL, NULL, NULL);

Step 2

Update: BusinessProfileController.php (or any other controller)

function upload_file($origin, $dest, $tmp_name, $overwrite=false,$nid=null){ FileManagerController::_checkPermission(); $max_upload_limit = ModuleSetting::findSetting('page', 'Max Img Size Upload'); $origin = basename($origin); $full_dest = $dest.$origin; $file_name = $origin; $uploaded_file_size = filesize($tmp_name); // Convert MB limit to bytes for comparison $max_upload_bytes = $max_upload_limit * 1024 * 1024; if ($uploaded_file_size > $max_upload_bytes) { Flash::set('error', __('Uploaded file exceeds the maximum allowed size of ' . $max_upload_limit . 'MB.')); return; } for ($i=1; file_exists($full_dest); $i++) { if ($overwrite) { unlink($full_dest); continue; } $file_ext = (strpos($origin, '.') === false ? '': '.'.substr(strrchr($origin, '.'), 1)); $file_name = substr($origin, 0, strlen($origin) - strlen($file_ext)).'_'.$i.$file_ext; $full_dest = $dest.$file_name; } // create new directory if (!file_exists($dest)) { if (mkdir($dest)) { chmod($dest, 0755); } } if (move_uploaded_file($tmp_name, $full_dest)) { //Add Image to database $data = isset($_POST['business_profile']) ? $_POST['business_profile'] : null; if ($data) { Flash::set('post_data', (object) $data); } $business_profile = BusinessProfile::findById($nid); if ($business_profile) { if (!$business_profile->update('BusinessProfile', array('image' => $file_name), 'id=' . $nid)) { Flash::set('error', __('Image has not been updated.')); } } // else { // Flash::set('success', __('Business Profile has been updated!')); // } // if (isset($_POST['commit'])) // redirect(get_url('business_profile/')); // else // redirect(get_url('business_profile/editbusinessprofile/'.$nid)); // change mode of the dire to 0644 by default chmod($full_dest, 0644); return $file_name; } return false; } // upload_file
function upload_file($origin, $dest, $tmp_name, $overwrite=false,$nid=null){
	FileManagerController::_checkPermission();
	$max_upload_limit = ModuleSetting::findSetting('page', 'Max Img Size Upload');
	$origin = basename($origin);
	$full_dest = $dest.$origin;
	$file_name = $origin;

	$uploaded_file_size = filesize($tmp_name);
	
	// Convert MB limit to bytes for comparison
	$max_upload_bytes = $max_upload_limit * 1024 * 1024;

	if ($uploaded_file_size > $max_upload_bytes) {
		Flash::set('error', __('Uploaded file exceeds the maximum allowed size of ' . $max_upload_limit . 'MB.'));
		return;
	}

	for ($i=1; file_exists($full_dest); $i++)
	{
		if ($overwrite) {
			unlink($full_dest);
			continue;
		}
		$file_ext = (strpos($origin, '.') === false ? '': '.'.substr(strrchr($origin, '.'), 1));
		$file_name = substr($origin, 0, strlen($origin) - strlen($file_ext)).'_'.$i.$file_ext;
		$full_dest = $dest.$file_name;
	}

	// create new directory
	if (!file_exists($dest)) {
		if (mkdir($dest)) { chmod($dest, 0755); }
	}

	if (move_uploaded_file($tmp_name, $full_dest))
	{
		//Add Image to database
		$data = isset($_POST['business_profile']) ? $_POST['business_profile'] : null;
		if ($data) {
			Flash::set('post_data', (object) $data);
		}

		$business_profile = BusinessProfile::findById($nid);
		if ($business_profile) {
			if (!$business_profile->update('BusinessProfile', array('image' => $file_name), 'id=' . $nid)) {
				Flash::set('error', __('Image has not been updated.'));
			}
		}
		// else {
		// 	Flash::set('success', __('Business Profile has been updated!'));
		// }

		// if (isset($_POST['commit']))
		// 	redirect(get_url('business_profile/'));
		// else
		// 	redirect(get_url('business_profile/editbusinessprofile/'.$nid));
			
		// change mode of the dire to 0644 by default
		chmod($full_dest, 0644);

		return $file_name;
	}

	return false;
} // upload_file
Code Copied To Clipboard!