在db中上传多个file_name

时间:2014-11-22 04:39:15

标签: php mysql codeigniter upload

您好我想上传多个文件并在db中存储多个file_name。 它是正确上传文件但不在一列中存储多个file_name, 例如如果我上传两个文件,所以它将两个文件名存储在两个新列中,但我想将它们存储在同一列中。

注意: 这是我用来上传file_name的代码

foreach($filepath as $a) {
            $a['file_name'].=",";
            echo $a['file_name'];
$db_data = array('pic_id'=> NULL,
'ad_pic'=> $a['file_name']);
$this->db->insert('pics',$db_data);
    }

我的控制器

<?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('first_body');
        $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';
    $this->load->library('upload', $config);


    if($this->input->post('submit'))
    {
         $path = './uploads';
        if($this->upload->do_multi_upload("myfile")){
            echo '<pre>';
            print_r($this->upload->get_multi_upload_data());
            echo '</pre>';


        $this->load->model('mypost');
        $this->mypost->post();
      }
    }else{
        $this->load->view("main_temp/header.php");
        $this->load->view('upload_f');
        $this->load->view("main_temp/footer.php");
    }
    }

    }

?>

我的模特

<?php
class mypost extends CI_Model
{

    function post()
    { 
        $filepath =$this->upload->get_multi_upload_data('file_name'); 

foreach($filepath as $a) {
            $a['file_name'].=",";
            echo $a['file_name'];
$db_data = array('pic_id'=> NULL,
'ad_pic'=> $a['file_name']);
$this->db->insert('pics',$db_data);
    }
        $title=$_POST['title'];
        $description=$_POST['description'];

        $category=$_POST['category'];
        $this->db->trans_begin();

        $this->db->query("insert into post (title,detail)
         values ('$title','$description')");


        $ad_id = $this->db->insert_id();

        $this->db->query("insert into category (cat_type,post_id_fk)
 values ('$category', $ad_id)");

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
      //  echo "<h1> Hurrah! </h1> <br> Data has been saved";
            $this->db->trans_commit();
        }
    }

}
?>

2 个答案:

答案 0 :(得分:0)

假设$this->upload->get_multi_upload_data('file_name')返回一个数组,它在数据库中创建多行的原因是因为你为每个返回的文件名运行$this->db->insert('pics', $db_data);

相反,尝试将数据保存到在foreach循环之外建立的变量,并在循环完成后将其发布到db,因此:

$filepath = $this->upload->get_multi_upload_data('file_name'); 

$filenames = ''

foreach($filepath as $a) {
    $filenames .= $a['file_name'].",";
}
$filenames = rtrim($filenames,',');


$db_data = array(
    'pic_id'=> NULL,
    'ad_pic'=> $filenames
);
$this->db->insert('pics', $db_data);

答案 1 :(得分:0)

我在现有代码中做了一些改动,无法将文件名filename1,filename1转换为filename1,filename2

foreach($filepath as $a) {
    $filenames .= $a['file_name'].",";
}
$filenames = rtrim($filenames,','); // I have make changes here

此代码将删除文件名字符串末尾不必要的(逗号)。