Fahim Sultan
Fahim Sultan

Reputation: 387

Set Max Size in CodeIgniter File Upload

I've used this private function to file upload in CodeIgniter Project. I've never set max_size in this function. Here, how to set max_size in this private function?

private function do_upload($value){
    $type = explode('.', $_FILES[$value]["name"]);
    $type = $type[count($type)-1];
    $url = "./assets/uploads/products/".uniqid(rand()).'.'.$type;

    if(in_array($type, array("jpg","jpeg","gif","png")))

    if(is_uploaded_file($_FILES[$value]["tmp_name"]))

    if(move_uploaded_file($_FILES[$value]["tmp_name"], $url))

    return $url;

    return "";
}

Upvotes: 1

Views: 15160

Answers (5)

TV-C-1-5
TV-C-1-5

Reputation: 714

CodeIgniter 4 - set max size using 'max_size[]' in the validation rules arary of your controller:

public function upload(){
        $validationRule = [
            'userfile' => [
                'label' => 'Image File',
                'rules' => [
                    'uploaded[userfile]',
                    'is_image[userfile]',
                    'mime_in[userfile,image/jpg,image/jpeg,image/gif,image/png,image]',

                    'max_size[userfile,100]',// <<< Max file size here

                    'max_dims[userfile,1024,768]',
                ],
            ],
        ];

if (! $this->validate($validationRule)) {
   $data = ['errors' => $this->validator->getErrors()];
   return view('back_end/owners/upload_form', $data);
 }

}

See CodeIgniter 4 docs: https://codeigniter.com/user_guide/libraries/validation.html?highlight=max_size

Upvotes: 0

Bang Andre
Bang Andre

Reputation: 541

AS recommend: https://codeigniter.com/userguide3/libraries/file_uploading.html 2MB (or 2048 KB) So that:

$config['max_size'] = 1024 * 10; // <= 10Mb;

Upvotes: 1

Swarna Sekhar Dhar
Swarna Sekhar Dhar

Reputation: 548

you can create a php.ini file at your websites base folder

and add following (2M = 2 megabyte you can add your desired size )

upload_max_filesize = 2M;

on it

Upvotes: 0

curiosity
curiosity

Reputation: 844

just simply do this.

if($_FILES["my_file_name"]['size'] > 1900000){  //set my_file_name to yours <input type="file" name="my_file_name">

  echo "file is too large";

}else{

  echo "file meets the minimum size";   

}

if you want to set bigger upload size to your system, you need to go to php.ini file and change this.

upload_max_filesize = 2M; //change this to your desired size.

hope that helps.

Upvotes: 2

R.tbr
R.tbr

Reputation: 310

You can get size of that file by $_FILES['uploaded_file']['size'], for example:

$maxsize=2097152;
if(($_FILES['uploaded_file']['size'] >= $maxsize)) {
        $errors[] = 'File too large. File must be less than 2 megabytes.';
    }

Upvotes: 2

Related Questions