• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Upload library issue

Hi. I'm having a bit of an issue with the upload library, and I would appreciate some help with it.
In essence, if I try to upload files from several inputs in the same function, I get either of two errors, depending on how I initialize the upload of any input following the first one.
If I initialize it using $this->upload->initialize($config) or $this->upload->initialize($config,TRUE), and the $config array for the previous input has something set up for the first input (ie allowed_types) while the second one shouldn't (ie if the second input shouold accept any file type), I get the following error:
You have not specified any allowed file types.

The filetype you are attempting to upload is not allowed.
And if I have more than one input (or more than one file, in an input with the multiple flag on), it shows the same error several times incrementally per input/file.

If, on the other hand, I initialize it using $this->upload->initialize($config,FALSE), assuming the previous one had $config['allowed_types'] = "jpg", the second one would only accept jpgs, and would return the following error if something else was provided, even if it should accept any file type:
The filetype you are attempting to upload is not allowed.

While what I'm making is a bit more complex than this, a barebones example would look like this.
Let's say I have two file inputs.
The first one, name="idcard", is a common file input, and should only take an image up to 10MB in filesize.
The second one, name="portfolio", has a multiple="multiple" flag set, should take any file type but up to 10MB in filesize, and would be limited to 20 files.
The controller looks like this:
function uptest() {
    $messages = array();
    if ($this->input->post('submitupload') !== NULL):
        //validation and database stuff skipped

        $timestamp = time();

        //idcard input:
        $config['allowed_types'] = "jpg|jpeg|png|gif|tiff";
        $config['max_size'] = 10240;
        $config['upload_path'] = FCPATH."../media/".$timestamp."/idcard";
        if ($this->upload->do_upload('idcard')):
            $messages['success_messages'][] = $_FILES['idcard']['name']." loaded correctly";
            $messages['upload_errors'][] = "idcard - ".$_FILES['idcard']['name']." - ".$this->upload->display_errors();

        //resetting $config
        $config = array();

        //portfolio input:
        $portfolio = $_FILES['portfolio'];
        $portfolioC = count($portfolio['tmp_name']);
        if ($portfolioC <= 20):
            $config['max_size'] = 10240;
            $config['upload_path'] = FCPATH."../media/".$timestamp."/portfolio";
            $this->upload->initialize($config,FALSE); //or $this->upload->initialize($config), which gives a different error
            for ($i=0;$i<$portfolioC;$i++):
                $_FILES['portfolio'] = array();
                $_FILES['portfolio']['tmp_name'] = $portfolio['tmp_name'][$i];
                $_FILES['portfolio']['name'] = $portfolio['name'][$i];
                $_FILES['portfolio']['type'] = $portfolio['type'][$i];
                $_FILES['portfolio']['size'] = $portfolio['size'][$i];
                $_FILES['portfolio']['error'] = $portfolio['error'][$i];
                if ($this->upload->do_upload('portfolio')):
                    $messages['success_messages'][] = $_FILES['portfolio']['name']." loaded correctly";
                    $messages['upload_errors'][] = "portfolio - ".$_FILES['portfolio']['name']." - ".$this->upload->display_errors();
            $messages['error_messages'] = "You can only upload up to 20 portfolio files";

        //rollback stuff skipped
} //function uptest
The relevant snippet of the view (views/test/uptest.php) looks like this:
    //skipping other messages, such as validation_errors
    if (isset($upload_errors)):
        foreach ($upload_errors AS $message):
            ?><div class="error"><?php echo $message?></div><?php echo "\n";
    if (isset($success_messages)):
        foreach ($success_messages AS $message):
            ?><div class="success"><?php echo $message?></div><?php echo "\n";
    echo form_open_multipart('uptest');
    ?>ID card: <input type="file" name="idcard" required="required" /><br /><?php echo "\n";
    ?>Portfolio: <input type="file" name="portfolio[]" multiple="multiple" /><br /><?php echo "\n";
    ?><input type="submit" name="submitupload" value="Submit" /><br /><?php echo "\n";
    echo form_close();
So, if I have the second upload initialization (line 30 in the controller snippet) as $this->upload->initialize($config,FALSE), I get a "The filetype you are attempting to upload is not allowed" error if I have anything but images in the second input. If I have a mix of images and other file types, it would upload the images, but not the rest, for which it would return said error.
If I have it as $this->upload->initialize($config) or $this->upload->initialize($config,TRUE), I get a "You have not specified any allowed file types. The filetype you are attempting to upload is not allowed" error regardless, returning it an incremental number of times per file.

Any help would be much appreciated. Thanks in advance.

No one?
In the meantime, I tried three things:
1 - unsetting the upload library and setting it again (as
after the first upload and then loading it before the loop on the second one),
2 - creating an unload extension to the loader, using the first answer here, https://stackoverflow.com/questions/22209209 , and
3 - adding a third parameter when loading the class to apparently avoid collisions (by doing
and then calling
, and then the same for the second input, but as "portfolioup")

... and no cigar.
Same "The filetype you are attempting to upload is not allowed. You have not specified any allowed file types." error, except for the loader extension, which returns "Undefined property: MY_Loader::$_ci_loaded_files" instead.

So yeah, any help would be really appreciated. To make it easier to test for anyone willing to, I isolated the controller and the view, which can be downloaded here:
Copying them to /controllers and /views as in the folders and accessing //:localhost/project/uptest should have it running, errors and all.

... and I solved it.
Turns out allowed_types appears to be required (had the brilliant idea to comment out the allowed_types line to see what errors I got, and the same one popped up for the first upload too), so as I wanted to accept any file type in the second input, I had to set
$config['allowed_types'] = '*'
before running the second upload (ie between lines 30 and 31 in the previously pasted controller code). Other options, such as file size, don't present that issue.
A single argument had me running like a headless chicken for a few days. Dang.
Anyway, it's solved now. Sorry about the clutter.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.