上传文件并在codeigniter中保存mysql的路径

时间:2016-10-01 00:06:51

标签: php mysql codeigniter file-upload

我有一个实现上传文件的字段表单。如何制作上传文件并将路径名保存到数据库?我需要它,因为我也必须下载链接。现在,我已成功上传文件,但它仍然保存了数据库中上传文件的名称而不是上传文件的路径。 也许我是否下载链接下载已上传的文件? 谢谢。我真的很感激。

这是我的控制者:

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

class Upload extends CI_Controller {

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

 public function index()
 {
  $this->load->view('v_upload');
 }

 function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpeg|png|gif|jpg|txt|docs|xls';
        $config['max_size'] = '2000';
        $config['max_width']  = '2000';
        $config['max_height']  = '2000';

        $this->load->library('upload', $config);

        $file1=($_FILES['file1']['name']);
        $file2=($_FILES['file2']['name']);
        $file3=($_FILES['file3']['name']);


        $file = array (
        'rab' => $file1,
        'kkp' => $file2,
        'rcps' => $file3);

        foreach ($_FILES as $key => $value) {
            if (!empty($value['tmp_name'])) {

                if ( ! $this->upload->do_upload($key)) {
                    $error = array('error' => $this->upload->display_errors());
                    //failed display the errors
                } else {
                    //upload success
                }

            }
        }
        if ($this->upload->do_upload($key)) {
        $this->file->input_data($file,'upload');
        }
 }
}

这是我的观点:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<HTML>
<body>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" />
<input type="file" name="file2"/>
<input type="file" name="file3"/>
</div>
<br /><br/>
<input type="submit" value="upload" name="upload" />
</form>
</body>
</HTML>

这是我的模特:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Model{

    function input_data($data,$table){
        $this->db->insert($table,$data);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试: $file1=($config['upload_path'].$_FILES['file1']['name']);

为您上传的每个文件。