Welcome Guest, Not a member yet? Register   Sign In
multiple upload with different configs problem
#1

[eluser]woony[/eluser]
hello,

I'm pretty new to codeigniter, and I love it. Now I am trying to make a multiple upload form. First one is an image and I'm making a thumbnail of it. Second is a pdf.
Now I'm trying to upload the pdf, first i was getting errors , but after using the initialize function that got fixed, so everything is working great, I'm getting no errors, my filename is inserted into the db BUT! my file is missing... it is not in the /attachments/ folder which has all rights to 777...
anyone knows why this is not working?

Below you can find my function

Code:
function create()
    {
        $this->load->model('news_model');

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '3000';
        $config['max_width']  = '3000';
        $config['max_height']  = '2000';

        $this->load->library('upload', $config);
        $filecounter = 0;
        foreach($_FILES as $key => $value)
        {
            $filecounter = $filecounter +1;
            if($filecounter == 1)
            {
                if ( ! $this->upload->do_upload($key))
                {
                    $errors = array('errors' => $this->upload->display_errors());
                    $this->load->view('news_maker', $errors);
                }    
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    $fInfo = $this->upload->data();
                    $this->_createThumbnail($fInfo['file_name']);
                    $data['uploadInfo'] = $fInfo;
                    $data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
                    $data['attachmentfile'] = "";
                }
            }
            elseif($filecounter == 2) //second file is our attachment and is not necesarry
            {
                if($_FILES[$key]['name'] == "")
                {
                    

                    $config['upload_path'] = './attachments/';
                    $config['allowed_types'] = 'pdf';
                    $config['max_size']    = '5000';
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if ( ! $this->upload->do_upload($key))
                    {
                        $errors = array('errors' => $this->upload->display_errors());
                        $this->load->view('news_maker', $errors);
                    }    
                    else
                    {

                        $data['attachupload'] = $this->upload->data();
                        $aInfo = $this->upload->data();
                        $data['attachinfo'] = $aInfo;
                        $data['attachmentfile'] = $aInfo['raw_name'] . $aInfo['file_ext'];
                    }
                }
                else
                {
                    $data['attachmentfile'] = "";
                }

                
            }
        }
            $newsdata = array(
            'title' => $this->input->post('title'),
            'date' => $this->input->post('datepicker'),
            'poster' => $this->session->userdata('username'),
            'text' => $this->input->post('content'),
            'imageUrl' => $data["upload_data"]["file_name"] ,
            'imageThumb' => $data['thumbnail_name'],
            'partnerID' => $this->input->post('partners'),
            'attachment' => $data["attachmentfile"]
        );
                $this->news_model->add_record($newsdata);
                $this->index();
        

    }
#2

[eluser]Craig300[/eluser]
Hi,

Try this...

Before the if statement, include the library definition
Code:
$this->load->library('upload');

Move your initial upload config values into the first part of the if statement then after this run the initialisation code

Code:
$this->upload->initialize($config);

So inside both parts of the if statement, you would have your $config declarations then you would initialize them. I also noticed that you were trying to load the upload library twice in the same function, if you call it at the top of the function, you don't need to call it further down.

Hope this helps Smile
#3

[eluser]woony[/eluser]
[quote author="Craig300" date="1266448043"]Hi,

Try this...

Before the if statement, include the library definition
Code:
$this->load->library('upload');

Move your initial upload config values into the first part of the if statement then after this run the initialisation code

Code:
$this->upload->initialize($config);

So inside both parts of the if statement, you would have your $config declarations then you would initialize them. I also noticed that you were trying to load the upload library twice in the same function, if you call it at the top of the function, you don't need to call it further down.

Hope this helps Smile[/quote]

Great! thank you so much, it fixed the problem, still making errors because of stuff like this. I'm really getting fond of this framework Smile
thanks for the help!
#4

[eluser]woony[/eluser]
Ok,
it seems it didnt help afterall, didnt do enough testing ...
I didnt got any more erros but in my else if I used = if($_FILES[$key]['name'] == "")
{

that should be != , if there is a file do the upload.
I'm still getting The filetype you are attempting to upload is not allowed.
this is a bit difrent cause its for another project but both need a multiple upload code... I really don't see it...

Code:
function create()
    {
        $this->load->model('reference_model');
        $this->load->library('upload');
        $filecounter = 0;
        foreach($_FILES as $key => $value)
        {
            $filecounter = $filecounter +1;
            if($filecounter == 1)
            {
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png|pdf';
                $config['max_size']    = '3000';
                $config['max_width']  = '3000';
                $config['max_height']  = '2000';
                $this->upload->initialize($config);

                if ( ! $this->upload->do_upload($key))
                {
                    $errors = array('errors' => $this->upload->display_errors());
                    $this->load->view('ref_maker', $errors);
                }    
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    $fInfo = $this->upload->data();
                    $this->_createThumbnail($fInfo['file_name']);
                    $data['uploadInfo'] = $fInfo;
                    $data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
                    $data['attachmentfile'] = "";
                }
            }
            elseif($filecounter == 2) //second file is our attachment and is not necesarry
            {
                if($_FILES[$key]['name'] != "")
                {
                    $config['upload_path'] = './attachments/';
                    $config['allowed_types'] = 'pdf';
                    $config['max_size']    = '5000';
                    $this->upload->initialize($config);

                    if ( ! $this->upload->do_upload($key))
                    {
                        $errors = array('errors' => $this->upload->display_errors());
                        $this->load->view('ref_maker', $errors);
                    }    
                    else
                    {
                        $data['attachupload'] = $this->upload->data();
                        $aInfo = $this->upload->data();
                        $data['attachinfo'] = $aInfo;
                        $data['attachmentfile'] = $aInfo['raw_name'] . $aInfo['file_ext'];
                    }
                }
                else
                {
                    $data['attachmentfile'] = "";
                }

                
            }
        }
            $refdata = array(
            'Title' => $this->input->post('title'),
            'Quote' => $this->input->post('quote'),
            'Description' => $this->input->post('content'),
            'imageUrl' => $data["upload_data"]["file_name"] ,
            'imageThumb' => $data['thumbnail_name'],
            'Fiche' => $data["attachmentfile"]
        );
                $this->reference_model->add_record($refdata);
                $this->index();
    }
#5

[eluser]woony[/eluser]
ok,
i got it fixed with this:
http://ellislab.com/forums/viewthread/70335/P15/
#6

[eluser]Craig300[/eluser]
Just out of curiousity, what type of file were you trying to upload?
#7

[eluser]woony[/eluser]
[quote author="Craig300" date="1266478190"]Just out of curiousity, what type of file were you trying to upload?[/quote]

as you can see in the above thread, it was also while uploading pdf, I never got any type in for those files, since this is a minor project , and only a few people have to work on it, I just make the type application/pdf for the second file. Also added application/unknown in the mimes just in case. It works for now, but it is not the best solution probably.

Still thanks for your way of initializing, it's alot better than what I had.
#8

[eluser]Craig300[/eluser]
If had a problem uploading pdf file and checked the mime types and couldn't see a problem. Actually found the problem to be that the pdf extension had to be placed in front of the other file extensions or it wouldn't work, like this

Code:
$config['allowed_types'] = 'pdf|gif|jpg|png';

No reason why but it worked after I done that. Must think it's superior to the other file types or something Smile
#9

[eluser]woony[/eluser]
[quote author="Craig300" date="1266512627"]If had a problem uploading pdf file and checked the mime types and couldn't see a problem. Actually found the problem to be that the pdf extension had to be placed in front of the other file extensions or it wouldn't work, like this

Code:
$config['allowed_types'] = 'pdf|gif|jpg|png';

No reason why but it worked after I done that. Must think it's superior to the other file types or something Smile[/quote]

yea, I read that aswell, but my 2nd config only had 'pdf' and it still didn't work.
The reason of the pdf thingy in the first place is because in the upload class, it checks the types if its gif etc, it checks if it is a image file and fails on a pdf. When it first checks for pdf, it goes through without checking it for being an image. Or something like that Smile




Theme © iAndrew 2016 - Forum software by © MyBB