Get filename as array after upload in codeigniter

99封情书 提交于 2019-12-12 03:15:13

问题


I'm facing an issue when returning the filename after uploading the files. When I press the submit button its uploading the image to folder but I'm not getting the filename.

This is the code:

CONTROLLER:

public function add()
    {
        $title = $this->input->post('title');
        $files = $this->do_upload();
        //print_r($files);
        $this->user->in($title,$files);
    }


public function do_upload()
    {       

        $name_array = array();
        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i<=$cpt-1; $i++)
        {           
           $_FILES['userfile']['name']= $files['userfile']['name'][$i];
           $_FILES['userfile']['type']= $files['userfile']['type'][$i];
           $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
           $_FILES['userfile']['error']= $files['userfile']['error'][$i];
           $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

           $this->upload->initialize($this->set_upload_options());
           $data = $this->upload->do_upload();
           $name_array[] = $data['file_name'];
        }

        $names = implode(',', $name_array);
        return $names;
     }

private function set_upload_options()
     {   
        $config = array();
        $config['upload_path'] = './portfolio/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;

        return $config;
     }  

MODEL:

public function in($title,$file)
     {
        foreach ($title as $key => $n) {

                        $insert[] = array(
                                'title' => $n,
                                'file' => $file[$key]
                                );
            }
            $this->db->insert_batch('title',$insert);
     }  

VIEW:

<?php echo form_open_multipart('location/add'); ?>
<div>
  <input type="text" name="title[]"><br>
  <input type="file" name="userfile[]">
</div>
<br><br>
<div>
  <input type="text" name="title[]"><br>
  <input type="file" name="userfile[]">
</div><br>
<input type="submit" name="" value="enter">
<?php echo form_close(); ?>  

And if I try to print the $files as print_r($files); its simply showing a , only.

Ref link: Multiple files upload (Array) with CodeIgniter 2.0


回答1:


$this->upload->do_upload() returns only true or false. You have to get data with $name_array[] = $this->upload->data('file_name');



来源:https://stackoverflow.com/questions/39894358/get-filename-as-array-after-upload-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!