2016-05-10 111 views
-2

上传库笨验证形式

function upload($upload_path = '', $file_name = ''){ 
    $config = $this->config($upload_path); 
    $this->CI->load->library('upload' ,$config); 

    //thuc hien upload 
    if($this->CI->upload->do_upload($file_name)){ 
     $data = $this->CI->upload->data(); 
    } 
    else{ 
     //khong upload thanh cong 
     $data = $this->CI->upload->display_error(); 
    } 
    return $data; 
} 
function config($upload_path = ''){ 
    //Khai bao bien cau hinh 
    $config = array(); 
    //thuc mục chứa file 
    $config['upload_path'] = $upload_path; 
    //Định dạng file được phép tải 
    $config['allowed_types'] = 'jpg|png|gif'; 
    //Dung lượng tối đa 
    $config['max_size']  = '500'; 
    //Chiều rộng tối đa 
    $config['max_width']  = '1500'; 
    //Chiều cao tối đa 
    $config['max_height'] = '1500'; 
    //load thư viện upload 
    $this->CI->load->library('upload', $config); 
    return $config; 
} 

编辑操作

function edit(){ 
    $id = $this->uri->rsegment('3'); 
    $news = $this->news_model->get_info($id); 

    if(!$news){ 
     $this->session->set_flashdata('message', 'Không tồn tại bài viết này'); 
     redirect(admin_url('news')); 
    } 
    $this->data['news'] = $news; 

    $this->load->library('form_validation'); //load thư viện 
    $this->load->helper('form'); //load helper 
    /*===============================================validate và update data===========================================*/ 
    if($this->input->post()){ 
     $this->form_validation->set_rules('title' ,'Tiêu đề bài viết ','required'); 
     $this->form_validation->set_rules('content' ,'Nội dung bài viết','required'); 

     if($this->form_validation-> run()){ 
      $this->load->library('upload_library'); 
      $upload_path = './upload/news'; 
      $upload_data = $this->upload_library->upload($upload_path, 'image'); 
      $image_link = $this->news_model->get_info($id, 'image_link') ; 

      if(isset($upload_data['file_name'])){ 
        $image_link = $upload_data['file_name']; 
      } 

      $data = array(
       'title'  => $this->input->post('title'), 
       'content' => $this->input->post('content'), 
       'image_link' => $image_link, 
       'meta_desc' => $this->input->post('meta_desc'), 
       'meta_key' => $this->input->post('meta_key'), 
       'created' => now() 
      ); 

      if($this->news_model->update($news->id , $data)){ 
       $this->session->set_flashdata('message', 'Cập nhật dữ liệu thành công'); 
      } 
      else{ 
       $this->session->set_flashdata('message', 'Không cập nhật dữ liệu thành công'); 
      } 
      //chuyển tới trang danh sách quản trị viên 
      redirect(admin_url('news')); 
     } 

    } 
    $this->data['temp'] = 'admin/news/edit'; 
    $this->load->view('admin/main' , $this->data); 
} 

http://i.stack.imgur.com/7UfmI.jpg

我编辑的数据不需要上传照片 当我点击更新按钮不上传文件时出错

http://i.stack.imgur.com/TE5lx.jpg

+0

发生了什么样的错误? – Amous

+0

致命错误:未捕获TypeError:传递给CI_Exceptions :: show_exception()的参数1必须是异常实例,在给出的错误实例中,在第658行的C:\ xampp \ htdocs \ webproduct \ system \ core \ Common.php中调用并在C:\ xampp \ htdocs \ webproduct \ system \ core \ Exceptions.php中定义:190 Stack trace:#0 C:\ xampp \ htdocs \ webproduct \ system \ core \ Common.php(658):CI_Exceptions-> show_exception (错误))#1 [内部函数]:_exception_handler(Object(Error))#2 {main}抛出C:\ xampp \ htdocs \ webproduct \ system \ core \ Exceptions.php 190行 –

+0

PHP错误遇到的问题 严重性:错误 消息:未捕获TypeError:传递给CI_Exceptions :: show_exception()的参数1必须是异常实例,在C:\ xampp \ htdocs \ webproduct \ system \ core中调用的给出的错误实例\ Common.php在658行上定义在C:\ xampp \ htdocs \ webproduct \ system \ core \ Exceptions.php中:190堆栈跟踪:#0 C:\ xampp \ htdocs \ webproduct \ system \ core \ Common.php(658):CI_Exceptions-> show_exception (错误))#1 [内部功能]:_exception_handler(对象(错误))#2 {主}抛出 文件名:芯/ Exceptions.php 行号:190 回溯: –

回答

0
$data = $this->CI->upload->display_error(); 

这是错误的方法调用。它应该是这样的

$data = $this->CI->upload->display_errors(); 
+0

太棒了!非常感谢你 ! –