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

时间:2015-01-19 11:38:11

标签: php codeigniter

在我的上传表单中,我有多个链接和用户文件。我希望能够插入链接/ url与该表行上的图像匹配,到目前为止我所尝试的还没有用。我已将链接和用户文件转换为数组,图像上传正常。

在我的控制器中,您将能够看到我测试的模型。

我想知道当插入数据库时​​用户选择图像时,如果url / link输入的输入将与图像文件名匹配

,最好是什么?

提前感谢。如果你决定投票,你可以告诉我为什么欢呼!

横幅表格

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

控制器

<?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 个答案:

答案 0 :(得分:0)

在我的视图中,因为链接和图像在数组中,我不得不额外count($_POST['link']); 对于链接和图像我必须这样做$filename = $files['userfile']['name'][$i];如图所示 成功的一部分。

我花了一些时间来阅读更多的PHP手册,现在这个代码适用于多个上传 同样。

类上传扩展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;
}

}