如何使用codeigniter上传库在数据库中插入文件名

时间:2010-09-02 18:52:13

标签: codeigniter

使用此示例中的codeigniter上传库可以将文件上传到文件中。

但是,我想在数据库中发送文件名。任何人都可以举个例子吗?感谢

<?php

class Upload extends Controller {

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

function index()
{   
    $this->load->view('upload_form', array('error' => ' ' ));
}

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->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }   
    else
    {
        $data = array('upload_data' => $this->upload->data());

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

或者我们有其他选择吗?

4 个答案:

答案 0 :(得分:4)

您可以使用data()方法获取此内容:

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

请注意,您还可以获取其他上传数据,只需查看您拥有的内容:

print_r($upload_data);

答案 1 :(得分:2)

创建一个数组,其中包含您需要在数据库中上传的任何内容...然后将其插入数据库中,在其他地方插入这样的内容:

 //create array to load to database
                $insert_data = array(
                    'name' => $image_data['file_name'],
                    'path' => $image_data['full_path'],
                    'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
                    'tag' => $tag
                     );

          $this->db->insert('photos', $insert_data);//load array to database 

重要提示:数组的索引与表的列具有相同的名称!

也尝试使用您的模型,它将使您的代码更容易维护,阅读等 祝你好运!

答案 2 :(得分:0)

你可以这样使用:

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

答案 3 :(得分:0)

$config['upload_path'] = './assets/uploads/';  // Upload Path will be here
        $config['allowed_types'] = 'gif|jpg|png';
        $this->upload->initialize($config);

        if ($this->upload->do_upload('upload')) {
           $file_data = $this->upload->data();
           $file_name= $file_data['file_name']; //get file name of your uploaded file from here..

         }
         else { 

          echo "Error";
         } 

        $data = array(

        'image_name' =>  $file_data['file_name'], //insert your image name from here..
        );

        $this->add_model->image_upload($data); //that will go to the model function..
        }