在codeigniter中上传后将文件名作为数组 [英] Get filename as array after upload in codeigniter

查看:102
本文介绍了在codeigniter中上传后将文件名作为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上传文件后返回文件名时遇到问题。
当我按提交按钮上传图像到文件夹,但我没有得到文件名。

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.

这是代码:

CONTROLLER:
$ b

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: b

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

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

查看

<?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(); ?>  

如果我尝试打印 $ files as print_r($ files); 它只显示一个

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

参考连结:使用CodeIgniter 2.0的多个文件上传(数组)

推荐答案

$ this-> upload-> do_upload()仅返回true或false。您必须使用 $ name_array [] = $ this-> upload-> data('file_name');

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

这篇关于在codeigniter中上传后将文件名作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆