简体   繁体   中英

Codeigniter 3 - can I get image upload's url from 3 images?

Hello I use Codeigniter to upload 3 images at once and I can upload successfully but I don't know how to get the url from 3 images (I can get url from the last image only.) (I want to insert 3 images's url to database)

here is my code

Controller

 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 view

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

my success page view (I want to echo the url of 3 images in this page! but I get only 1 url of last image) (Actually I want to insert to database)

<h1>Successfully Add</h1>

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

I assume $this->upload->data() take the information of the last uploaded file,is not that?Can you do a print_r($upload_data) ? Or Can you try something like this?

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

$this->upload->data() is last uploaded data so please try

........

$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>

At this stage you are uploading 3 files but only get data of last inserted file.

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

you should get data of every file and push to array. like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM