Welcome Guest, Not a member yet? Register   Sign In
extend the upload library, if file greater than post_max_size, it would return error message file exceeds limits, not "
#1

[eluser]searain[/eluser]
Current upload library, if file greater than post_max_size, it would return the misleading message, "you did not select a file to upload."

I extended the upload library, so it would return "file exceeds limits" error message.

Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Upload extends CI_Upload {

    function do_upload($field = 'userfile') {
        // If file greater than POST_MAX_SIZE. set_error('upload_file_exceeds_limit');
        // use codes from http://www.php.net/manual/en/features.file-upload.php#73762
        $POST_MAX_SIZE = ini_get('post_max_size');
      
        $mul = substr($POST_MAX_SIZE, -1);
        $mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
        if ($_SERVER['CONTENT_LENGTH'] > $mul * (int) $POST_MAX_SIZE && $POST_MAX_SIZE) {
            
            $this->set_error('upload_file_exceeds_limit');
            return FALSE;
        }

        return parent::do_upload($field);
    }

}

Any advices or suggestions?
#2

[eluser]RogerMore[/eluser]
Hey Searain,

Thanks for your post.

Some time ago I had a similar problem. The only difference is that you al least got the misleading message. After I moved my code to the production server, the upload page didn't work anymore. When the form was submitted, an empty form was returned. No error, no previous data in the new form, nothing.. Eventually I had to read the server logs to find out that the post_max_size was to low.

In my opinion this has to be fixed by the guys from codeigniter. My only advice is to let them know about this problem, maybe through the bugtracker or whatever.

It still amazes me that you are the only one I could find with this problem (and a temporary solution). I think I will use this in my next project when this problem isn't fixed.

Greetings,

Roger




Theme © iAndrew 2016 - Forum software by © MyBB