Search code examples
phpcodeigniterfile-upload

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 "";
}

Solution

  • 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.';
        }