【问题标题】:Image is not uploading properly in codeigniter图像未在 codeigniter 中正确上传
【发布时间】:2016-03-15 18:00:29
【问题描述】:

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

我查看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');                       
            }
}

【问题讨论】:

    标签: image codeigniter upload


    【解决方案1】:
    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
    }
    

    【讨论】:

    • 确保您的根目录中有“图像”和允许访问图像文件夹的 .htacess 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多