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

#1
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:
Code:
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:
Code:
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:
Code:
function uptest() {
    $messages = array();
    if ($this->input->post('submitupload') !== NULL):
        //validation and database stuff skipped

        $this->load->library('upload');
        $timestamp = time();

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

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

        //portfolio input:
        $portfolio = $_FILES['portfolio'];
        $portfolioC = count($portfolio['tmp_name']);
        if ($portfolioC <= 20):
            $config['max_size'] = 10240;
            mkdir(FCPATH."../media/".$timestamp."/portfolio",0777,TRUE);
            $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";
                else:
                    $messages['upload_errors'][] = "portfolio - ".$_FILES['portfolio']['name']." - ".$this->upload->display_errors();
                endif;
            endfor;
        else:
            $messages['error_messages'] = "You can only upload up to 20 portfolio files";
        endif;

        //rollback stuff skipped
    endif;
    $this->load->view('test/uptest',$messages);
} //function uptest
The relevant snippet of the view (views/test/uptest.php) looks like this:
Code:
<?php
    //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";
        endforeach;
    endif;
    if (isset($success_messages)):
        foreach ($success_messages AS $message):
            ?><div class="success"><?php echo $message?></div><?php echo "\n";
        endforeach;
    endif;
    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.
Reply

#2
No one?
In the meantime, I tried three things:
1 - unsetting the upload library and setting it again (as
Code:
unset($this->upload)
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
Code:
$this->load->library('upload',$config,"idup")
and then calling
Code:
$this->idup->do_upload('idcard')
, 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:
https://a.pomf.cat/gonuie.zip
Copying them to /controllers and /views as in the folders and accessing //:localhost/project/uptest should have it running, errors and all.
Thanks.
Reply

#3
... 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
Code:
$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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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