Welcome Guest, Not a member yet? Register   Sign In
[Solved] Upload library issue
#1

(This post was last modified: 10-11-2018, 05:53 PM by f6342222.)

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


Messages In This Thread
[Solved] Upload library issue - by f6342222 - 10-10-2018, 02:28 PM
RE: Upload library issue - by f6342222 - 10-11-2018, 11:36 AM
RE: Upload library issue - by f6342222 - 10-11-2018, 05:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB