图像未在codeigniter中正确上传

时间:2016-03-15 18:00:29

标签: image codeigniter upload

我正在尝试上传图片,但只有内容正确插入数据库,但图片路径或文件未在此处上传我的代码,

我的观点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');                       
            }
}

1 个答案:

答案 0 :(得分: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
}