duan0417 2015-01-19 11:38
浏览 44
已采纳

插入数据URL /链接输入匹配图像上载的Codeigniter

On my upload form I have multiple links and userfiles. I would like to be able to insert the link/url where matches up with the image that is on that table row, so far what I have tried has not worked. I have made the link and userfile into array the images upload fine.

In my controller you will be able to see what I have tested for model.

What I would like to know what is best to make it so when the user selects the image when insert into data base that the input of the url/link input will match the image file name

Thank in advance. If you decided to vote down can you let me know why cheers!

Banner Form

<?php echo Modules::run('admin/common/header/index');?><?php echo Modules::run('admin/common/column_left/index');?>
<div id="content">
<div class="container-fluid">

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-right">
</div>
</div>
<div class="panel-body">
<?php echo $error;?>
<?php echo form_open_multipart('admin/design/banners/do_upload');?>

<table>
<thead>
<tr>
<td>Link</td>
<td>Image</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="link[]" placeholder="url"></td>
<td><input type="file" name="userfile[]" size="20" multiple="multiple" /></td>
</tr>
<tr>
<td><input type="text" name="link[]" placeholder="url"></td>
<td><input type="file" name="userfile[]" size="20" multiple="multiple" /></td>
</tr>
</tbody>
</table>

<input type="submit" value="upload" />

</form>
</div>
</div>

</div>
</div>
</div>
</div>

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banners extends MX_Controller {

public function __construct() {
    parent::__construct();
}

public function index() {

    $data['error'] = '';

    $this->load->view('design/banner_form.tpl', $data);

}

public function do_upload() {

$files = $_FILES;

$file_loop = count($_FILES['userfile']['name']);

for($i = 0; $i < $file_loop; $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->file_config());

if ($this->upload->do_upload() == FALSE) {

$data['error'] = $this->upload->display_errors();               
$this->load->view('design/banner_form.tpl', $data);

} else {

/*
| 
| Model Function Here For Tesing Only
| Will make proper model function when issue sorted.
|
*/

$upload_infos = $this->upload->data();

foreach ($upload_infos as $upload_info) {

$data = array(
'image' => $upload_info['file_name'],
//'link' => $this->input->post('link')
);

}

$this->db->insert('banner_image', $data);

return $this->load->view('design/banner_list.tpl', $data);

}

} 

}

private function file_config() {   

$config = array();

$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '3000';
$config['overwrite'] = TRUE;

return $config;

}

}
  • 写回答

1条回答 默认 最新

  • duanou1904 2015-01-20 05:20
    关注

    In my view because links and images were in array I had to make a extra count($_POST['link']); for the link and the for image I had to do this $filename = $files['userfile']['name'][$i]; as showen in success form part.

    It took me a while to figure it out after reading more of manual in php and now this code works for multple uploads as well.

    class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {
    
    
        $this->load->view('design/banner_form.tpl', array('error' => ' ' ));
    }
    
    function do_upload() {
    
        $files = $_FILES;
    
        $file_loop = count($_FILES['userfile']['name']);
    
        for($i= 0; $i < $file_loop; $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->file_config());
    
            if (!$this->upload->do_upload()) {              
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('design/banner_form.tpl', $error);
            } else {                
    
                $data_count = count($_POST['link']);
                $url = $_POST['link'][$i];
                $filename = $files['userfile']['name'][$i];                 
                $this->db->set('link', $url);
                $this->db->set('image', $filename);
                $this->db->insert('banner_image');
    
                $this->load->view('design/banner_list.tpl');
            }
        } 
    }
    
    private function file_config() { 
        $config = array();
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '0';
        $config['overwrite']     = TRUE;
    
        return $config;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料