Codeigniter 3 - 我可以从3张图片中获取图片上传的网址吗?

时间:2016-11-18 07:43:42

标签: php mysql codeigniter

您好我使用Codeigniter一次上传3张图片,我可以成功上传,但我不知道如何从3张图片中获取网址(我只能从最后一张图片中获取网址。)(我想要将3个图像的网址插入数据库)

这是我的代码

控制器

 public function add_event()
   {

 $config['upload_path']          = 'images/location';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 2048; 
    $config['max_width']            = 2000;
    $config['max_height']           = 1500;

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

 $this->upload->do_upload('userfile'); 
 $this->upload->do_upload('userfile2');
 $this->upload->do_upload('userfile3');

$data_upl = array('upload_data' => $this->upload->data());

$this->load->view('admin/success', $data_upl);
 }

myuploadform视图

        <?php echo form_open_multipart('admin/add_event') ?>

            pic1
            <input type="file" name="userfile" size="20" />

            pic2
            <input type="file" name="userfile2" size="20" />


            pic3
            <input type="file" name="userfile3" size="20" />

            <input type="submit" value="upload"/>
         <?php echo form_close(); ?>

我的成功页面视图(我希望在此页面中回显3个图像的网址!但我只得到最后一个图像的1个网址)(实际上我想插入数据库)

<h1>Successfully Add</h1>

<ul>
<li><?php echo $upload_data['full_path'];?></li>
</ul>

3 个答案:

答案 0 :(得分:3)

我假设$ this-&gt; upload-&gt; data()获取上次上传文件的信息,不是吗?你能做一个print_r($ upload_data)吗? 或者你可以试试这样的东西吗?

$upload_data=array();
$this->upload->do_upload('userfile'); 
$upload_data['1']= $this->upload->data());
 $this->upload->do_upload('userfile2');
$upload_data['2']= $this->upload->data());
 $this->upload->do_upload('userfile3');
$upload_data['3']= $this->upload->data());

答案 1 :(得分:2)

$ this-&gt; upload-&gt; data()是上次上传的数据,请尝试

........

$alldata=array();
$this->upload->do_upload('userfile'); 
$alldata[]=$this->upload->data();

$this->upload->do_upload('userfile2');
$alldata[]=$this->upload->data();

$this->upload->do_upload('userfile3');
$alldata[]=$this->upload->data();

$data_upl = array('upload_data' => $alldata);

.......

<h1>Successfully Add</h1>

<ul>
<li><?php print_r($upload_data);?></li>
</ul>

答案 2 :(得分:1)

在此阶段,您正在上传3个文件,但只获取最后插入文件的数据。

$this->upload->do_upload('userfile'); 
$this->upload->do_upload('userfile2');
$this->upload->do_upload('userfile3');
$data_upl = array('upload_data' => $this->upload->data());

你应该得到每个文件的数据并推送到数组。像

$data = array();
$data['upload_data'] = array();
$this->upload->do_upload('userfile'); 
$data['upload_data'][0] = $this->upload->data();
$this->upload->do_upload('userfile2');
$data['upload_data'][1] = $this->upload->data();
$this->upload->do_upload('userfile3');
$data['upload_data'][2] = $this->upload->data();