Codeigniter Multiple File Upload Encryption Issue

2019-09-15 04:43发布

问题:

I've got a big form that allows users to upload multiple files/filetypes to an offer/bid they are creating. Everything is working fine except one piece: the name encryption of the files before saving to the database.

I haven't found a rhyme or reason for it, but it's hit or miss. The image works fine every time. The other documents (which allow all [*] types, but primarily will consist of various business docs such as pdf, doc, xls, etc.) are the ones that are spotty.

I found threads on SO and elsewhere about general issues with the name encryption but have yet to come across one that deals with the specificity of my issue.

Here's the upload function:

//for multi uploads
        function do_uploads($name, $file)
        {
            $status ="";
            $msg = "";
            $file_element_name = $name;


            //go through and figure out where it goes
            if($name == "QuoteDoc") {
                $folder = "quotedocs";
                $allowed = '*';
            }
            else if($name == "ProductOfferPhoto") {
                $folder = "product_photos";
                $allowed = 'jpeg|jpg|png|gif';
            }
            else if($name == "ResearchWhtPaper1" || $name == "ResearchWhtPaper2") {
                $folder = "research";
                $allowed = "*";
            }
            else if($name == "ProductLiterature1" || $name == "ProductLiterature2") {
                $folder = "literature";
                $allowed = "*";
            }
            else if ($name == "FDALink") {
                $folder = "fda";
                $allowed = "*";
            }


            $config['upload_path'] = './uploads/' . $folder;
            $config['allowed_types'] = $allowed;
            $config['max_size']  = 1024 * 8;
            $config['encrypt_name'] = TRUE;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload($name))
            {
                $status = 'error';
                $msg = $this->upload->display_errors('', '');
            }
            else {
                $data = $this->upload->data();
            }

            @unlink($_FILES[$file_element_name]);

            //what's up?
            //return $this->upload->data();
            return array('status' => $status, 'msg' => $msg, 'data' => $this->upload->data(), 'allowed'=>$allowed);
        }

Any help would be greatly appreciated.

回答1:

You're not stating your question very clearly:

Are the names simply not being encrypted, but still uploading to the correct directories?

Are you setting these inside a loop, where perhaps the class instance is not being re-initialized? Does the first file encrypt correctly, but not the subsequent ones?

Can you track which file types are not working correctly?

I have trouble believing it is completely "random", and think there's just not enough research being done here


Solution from below:

You need to use $this->upload->initialize($config) to change the config, as the library will only be loaded once