2016-03-15 64 views
0

正确上传我试图上传的图像,但只有内容插入到数据库中正确但图像路径或文件不上载这里我的代码,图像不是笨

我的观点post.php中文件代码:

<form action="" method="post" enctype="multipart/form-data"> 
     <table> 
       <tr> 
        <td>Book Title</td> 
        <td><input type="text" name="title" /></td> 
       </tr> 
       <tr> 
        <td>Book Author</td> 
        <td><input type="text" name="author" /></td> 
       </tr> 
       <tr> 
        <td>Book Image</td> 
        <td><input type="file" name="image" /></td> 
       </tr> 

       <tr> 
        <td>Book Description</td> 
        <td><input type="text" name="content" /></td> 
       </tr> 
       <tr> 
        <td><input type="submit" name="submit" /></td> 
       </tr> 
     </table> 
     </form> 

这里我控制器的welcome.php文件代码:

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

class Welcome extends CI_Controller { 
    public function index() 
      {  
     if (!empty($_POST)) { 
      $title = $this->input->post('title'); 
      $author = $this->input->post('author'); 
      $image['upload_path'] = './images/'.$_FILES['image']['tmp_name']; 
      $image['allowed_types'] = 'gif|jpg|png'; 
      $image['max_size'] = '200'; 
      $image['max_width'] = '2000'; 
      $image['max_height'] = '1500'; 
      $this->load->library('upload', $image); 
      $content = $this->input->post('content'); 
      $date = date('Y-m-d'); 
      if ($title && $author && $image && $content) { 
       $this->load->model('insert'); 
       $data = array(
       //field list 
        'book_title' => $title, 
        'book_author' => $author, 
        'book_image' => $image['upload_path'], 
        'book_content' => $content, 
        'date' => $date 
       ); 
       $id = $this->insert->form($data); 
      } 

     } 
     $this->load->view('insert/post');      
      } 
} 

回答

0
try this code it will help you 
$filename='image'; //Your image name 
$title = $this->input->post('title'); 
$content = $this->input->post('content'); 
$author = $this->input->post('author'); 
$image['upload_path'] = '/images/'; 
$image['allowed_types'] = 'gif|jpg|png'; 
$image['max_size'] = '1024*4'; 
$image['max_width'] = '2000'; 
$image['max_height'] = '1500'; 
$image['encrypt_name']=true; 
$this->load->library('upload', $image); 
$date = date('Y-m-d'); 
if ($title && $author && $image && $content) { 
$this->load->model('insert'); 
$data=$this->upload->data(); 
$data1 = array(
//field list 
'book_title' => $title, 
'book_author' => $author, 
'book_content' => $content, 
'date' => $date 
); 
$id = $this->insert->form($data['file_name'],$data1); 

$this->load->view('insert/post');      
} 

Model Code: 
public function insert_file($filename,$title) 
{ 
    // your insert query 
} 
+0

请确保您的根目录和.htacess文件中有'images',以允许访问images文件夹。 – Himanshu

0

我会尽力纠正你控制器。

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

class Welcome extends CI_Controller { 

    public function index() 
    { 
     if (!empty($_POST)) { 
      $title = $this->input->post('title'); 
      $author = $this->input->post('author'); 
      $config['upload_path'] = './images/'; //only upload path 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '200'; 
      $config['max_width'] = '2000'; 
      $config['max_height'] = '1500'; 
      $this->load->library('upload', $config); //upload lib config not image 
      $content = $this->input->post('content'); 
      $date = date('Y-m-d'); 
      if ($title && $author && $image && $content) { 
       if (! $this->upload->do_upload('image')) { //your file input name 
        $view_data['error'] = array('error' => $this->upload->display_errors()); //pass errror to view in case of error 
       } else { 
        $upload_data = $this->upload->data(); 
        $view_data['upload_data'] = $upload_data; //to pass upload data to view 

        $this->load->model('insert'); 
        $data = array(
         //field list 
         'book_title' => $title, 
         'book_author' => $author, 
         'book_image' => $upload_data['full_path'], //read the user_guide to see other posibilities 
         'book_content' => $content, 
         'date' => $date 
        ); 
        $id = $this->insert->form($data); 
       } 
      } 
     } 
     $this->load->view('insert/post', $view_data); //pass upload data or error 
    } 
} 
+0

我试图使用你的代码,但图像上传到根文件夹,但它没有插入到数据库中,它显示这个值请看这里: –

+0

'字段列表'中的未知列'file_name' INSERT INTO'bookdata'(' file_name,file_type,file_path,full_path,raw_name,orig_name,client_name,file_ext,file_size,is_image,image_width,image_height,image_type, ,'image_size_str')VALUES('test-image.jpg','image/jpeg','C:/ wamp/www/Book/images /','C:/ wamp/www/Book/images/test-image .jpg','test-image','test-image.jpg','test-image.jpg','.jpg',11.18,1,468,455,'jpeg','width = \“468 \ “height = \”455 \“') –

+0

您是否在将$ data更改为$ view_data之后使用了答案?如果image lib使用$ data变量,然后尝试将$ data插入到您的模型中,它认为还有许多其他列。 – cssBlaster21895