用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1553|回复: 3
收起左侧

[HELP] 上传文件怎么把取得的文件名加入数据库里

[复制链接]
发表于 2015-6-3 14:45:13 | 显示全部楼层 |阅读模式
控制器;
function dotu_add(){
                $config['upload_path'] = './uploads/';//上传的文件夹【要先建立】
                $config['allowed_types'] = 'gif|jpg|png';//允许上传文件类型
                $config['max_size'] = '300';//允许上传的最大容量
                $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());
                //echo "error";
                $this->load->view('upload_form.html', $error);
//加载错误信息页面
                }
                                else           {
                                //echo "ok";
                                $data = array('upload_data' => $this->upload->data());
//var_dump($data);
               
                // echo $data['upload_data']['file_name'];//这是上传的文件名称
                 //我们只要把这个数据入库就成了、
                   //$this->load->view('upload_success.html', $data);
                                }
                        //        $tupian="./uploads/".$data['upload_data']['file_name'];
                                $path=$config['upload_path'];
                                $file=$data['upload_data']['file_name'];
                                $this->wm_image($file,$path);
                                $this->do_image($file,$path);
$arr=array(
                                        "userfile"=>$this->input->post('userfile'),
                                        "title"=>$this->input->post('title'),
                                        "userfile"=>$this->input->post('file_name'),
                                               
                        );
                        $this->Admin_lei->add_tu($arr);
        }
        function do_image($file,$path){
                $this->load->library('image_lib');
                $config['image_library']='GD2';
                $config['source_image']=$path."/".$file;
                $config['maintain_ratio']=TRUE;
                $config['width']=75;
                $config['height']=50;
                $config['create_thumb']=TRUE;
                $config ['new_image'] = './uploads/thumb_' . $file;
                $this->image_lib->initialize($config);
                if(!$this->image_lib->resize()){
                        echo "failed";}else {
                                echo "success!";}
        }
        function wm_image($file,$path){
                $this->load->library('image_lib');
                $config['source_image'] = $path."/".$file;
                //$config['wm_overlay_path']='./uploads/logo.jpg';
                $config['wm_text'] = 'www.588000.net php家教网';
                $config['wm_font_path'] = './uploads/simhei.ttf';
                $config['wm_type'] = 'text';//添加文本水印,overlay为添加图片水印,text为文本水印
                //$config['wm_opacity'] = '50';//透明度
                $config['wm_vrt_alignment'] = 'bottom';
                $config['wm_hor_alignment'] = 'center';
                $config1['wm_padding'] = '-150';
                $config ['new_image'] = './uploads/water_' . $file;
                $this->image_lib->initialize($config);
                if(!$this->image_lib->watermark())
                {echo 'failure to watermark';}
                else
                {echo 'success';}
models
function add_tu($data){
                $this->db->insert('tu', $data);
        }
模型
<form action="<?php echo site_url('admin/tu_doadd')?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="600" border="1" align="center" cellpadding="9" cellspacing="1" class="form">
    <tr>
      <td colspan="2" align="center">上传图片:</td>
    </tr>
    <tr>
      <td width="44%" align="right">图片标题:</td>
      <td width="56%"><input type="text" name="title" id="title" /></td>
    </tr>
    <tr>
      <td align="right">上传图片:</td>
      <td><input type="file" name="userfile" id="userfile" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" name="button" id="button" value="提交" />
        &nbsp;
<input type="submit" name="button2" id="button2" value="提交" /></td>
    </tr>
  </table>

</form>
应该怎么写才能把上传图片的文件名写入数据库。

发表于 2015-6-3 14:58:42 | 显示全部楼层
看清手冊阿 $this->upload->data() 包含了上傳文件的所有訊息
CI 手冊 - 文件上傳類
 楼主| 发表于 2015-6-3 16:25:34 | 显示全部楼层
Closer 发表于 2015-6-3 14:58
看清手冊阿 $this->upload->data() 包含了上傳文件的所有訊息
CI 手冊 - 文件上傳類 ...

但我不知道怎么写。。。
发表于 2015-6-3 16:37:41 | 显示全部楼层
c79117 发表于 2015-6-3 16:25
但我不知道怎么写。。。

$this->upload->data() 存入一個變數
再將這個變數陣列裡面某個陣列值扔到資料庫 insert
你的問題在哪邊?

本版积分规则