how to upload different files (images & documents) in codeigniter

hi i'm new to codeigniter, can you please help me in insert multiple files(documents and images) in code igniter. here's my sample code

view:

<label>Picture</label>
            <input type="file" name="userfile" size="100" /> 

<label>Document</label>
           <input type="file" name="documentfile" size="10" />

controller:

 $m = $_FILES['userfile']['name'];
 $n = $_FILES['documentfile']['name'];
  if ($m !== "")
        {
            $config['upload_path'] = './upload_images/';
            $config['allowed_types'] = 'jpg|png|jpeg|gif';
            $config['max_size'] = '0'; // 0 = no file size limit
            $config['max_width']  = '0';
            $config['max_height']  = '0';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
             $this->upload->do_upload();
             $upload_result = $this->upload->data();
        }
       elseif ($n !== "")
        {
            $config_document['upload_path'] = './upload_documents/';
            $config_document['allowed_types'] = 'pdf';
            $config_document['max_size'] = '0';
            $config_document['overwrite'] = TRUE;
            $this->load->library('upload', $config_document);
             $this->upload->do_upload();
             $upload_result2 = $this->upload->data();
        }   
 $image_filename = $upload_result['file_name'];
 $docu_filename = $upload_result2['file_name '];
$this->MODEL->add_asset($image_filename, $docu_filename);

i tried to echo both file names and it works but my $docu_filename generates NULL value; please help. thank you

Upvotes: 0

Views: 2075

Answers (1)

umefarooq
umefarooq

Reputation: 4574

hi its simple just check the file extension of uploaded file and make settings according to that. one more thing you have to set your html form enctype also. check the following example

form View

<form method="post" action="controller" enctype="multipart/form-data"> 
    <input type="file" name="test">
    <input type="submit" value="submit" />
</form>

in controller

 $path = $_FILES['test']['name'];
 $ext = pathinfo($path, PATHINFO_EXTENSION);
 $img_ext_chk = array('jpg','png','gif','jpeg');
 $doc_ext_chk = array('pdf','doc');

if (in_array($ext,$img_ext_chk))
        {
            $config['upload_path'] = './upload_images/';
            $config['allowed_types'] = 'jpg|png|jpeg|gif';
            $config['max_size'] = '0'; // 0 = no file size limit
            $config['max_width']  = '0';
            $config['max_height']  = '0';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
             $this->upload->do_upload();
             $upload_result = $this->upload->data();
        }
       elseif (in_array($ext,$doc_ext_chk))
        {
            $config_document['upload_path'] = './upload_documents/';
            $config_document['allowed_types'] = 'pdf';
            $config_document['max_size'] = '0';
            $config_document['overwrite'] = TRUE;
            $this->load->library('upload', $config_document);
             $this->upload->do_upload();
             $upload_result2 = $this->upload->data();
        }  

Upvotes: 2

Related Questions