php - 尝试在 codeigniter 的一列中上传两个图像 file_path

标签 php mysql codeigniter

我正在尝试在一列中上传两个图像文件路径。在我的模型中,我从 $filepath 成功获取第一个图像路径,但 $a 不起作用,我也很困惑 $a 是否正在获取我的第二个图像路径

请帮助我

提前致谢。

我的 Controller

<?php

class upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view("main_temp/header.php");
        $this->load->view('post_ad_views', array('error' => ' ' ));
        $this->load->view("main_temp/footer.php");
    }

// controller
function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['file_name'] = $new_file_name;
    $this->load->library('upload', $config);


    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view("main_temp/header.php");
        $this->load->view('post_ad_views', $error);
        $this->load->view("main_temp/footer.php");
    }
    else
    {
        // success
        $data = array('upload_data' => $this->upload->data());

        // a model that deals with your image data (you have to create this)
        $this->load->model('submit_ad_model');
        $this->submit_ad_model->ad_post();

        $this->load->view('upload_success', $data);


   }


}
}
?>

我的模型

<?php
class submit_ad_model extends CI_Model
{

    function ad_post()
    { 

        $filepath = $this->upload->data()['file_name']; 

         $a = $this->upload->data()['file_name']; 

        $this->db->query("insert into ads (ad_pic)  values ('$filepath,$a')");
?>

我的观点

<input type="file" name="userfile" class="upload image"  id="userfile" />
<input type="file" name="userfile2" class="upload image"  id="userfile" />

最佳答案

您不想获取第二个上传的项目,您需要使用循环:

       $upload_data = array();
       foreach($_FILES as $key => $value){
           $this->upload->do_upload($key);
           $upload_data[$key] = $this->upload->data($key);
        }
         $this->submit_ad_model->ad_post($upload_data);

关于php - 尝试在 codeigniter 的一列中上传两个图像 file_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374727/

相关文章:

php - 在 Laravel 中验证用户时防止暴力攻击

php - 模组安全 : Access denied with code 403

php - 尝试使用 php 将 session 变量传递到 mysql 查询中

php - 在 Codeigniter 中上传 - 不允许您尝试上传的文件类型

php - Laravel 上的 OrderBy 多态一对一关系

php - 在 Woocommerce 3 中添加新产品类型

mysql - 如何计算等于或加起来等于某个时移的记录?

sql - 删除一个表中所有未被另一个表引用的记录

javascript - Codeigniter 购物车使用 jQuery 删除单个元素

php - CodeIgniter - 在 View 中访问 $config 变量