douzhang6496 2018-10-26 18:36
浏览 37

Codeigniter - 无法通过输入类型文件上传图像

I am creating recipe book in PHP Codeigniter. I have a problem with uploading image to resource folder and save image path to dtb. All the data successfully save to database except file (image). View.

 <?php echo form_open_multipart('Recipes/add'); ?>
       <table style="padding: 15px;">
    <tr>
        <td><label for="title">Title:</label></td>
        <td><input type="text" name="title" id="title" name="title"  required/></td>
    </tr>
    <tr>
        <td><label for="category">Category:</label></td>
        <td>
            <select id="category" name="category">
                <?php 
                    foreach($categories as $row1){
                        print "<option value=" . $row1->id . ">";
                        print $row1->title;
                        print "</option>";
                    }
                ?>
            </select> 
    </td>
    </tr>
    <tr>
        <td><label for="ingredients">Ingredients:</label></td>
        <td><textarea name="ingredients" id="ingredients" required></textarea></td>
    </tr>
    <tr>
        <td> <label for="production_method">Production method:</label></td>
           <td><textarea name="production_method" id="production_method" required></textarea></td>
    </tr>
    <tr>
        <td> <label for="production_time">Production time:</label></td>
          <td><input type="production_time" name="production_time" id="production_time"  required/> minutes</td>
    </tr>

     <tr>
       <td> <label for="image_path">Upload an image:</label></td>
          <td><input type="file"name="image_path"></td>
    </tr>
    <tr>
        <td>  <input type="submit" value="Save recipe" id="submit" name="submit"/></td>
        <td> <input type="reset" value="Reset" id="reset" /></td>
    </tr>
    </table>
    <?php echo form_close(); ?>

Controller

class Recipes extends CI_Controller{
     public function __construct(){     
        //call CodeIgniter's default Constructor
                parent::__construct();

                //load database libray manually
                $this->load->database();

                //load Model
                $this->load->model('Recipe_model');

                $this->load->helper(array('form', 'url'));

                //load registration view form
                $this->load->model("Category_model");

            }                     
            public function add(){
                $data['categories']= $this->Category_model->get_categories();
                //call a function of the loaded view
                $this->load->view("templates/header");
                //we load a view to display the results
                $this->load->view("new_insert",$data);
                $this->load->view("templates/footer");

                    $config['upload_path'] = "/CI/assets/imgs/";
                    $config['allowed_types'] = "gif|jpg|png|jpeg";
                    $this->load->library('upload', $config);


                //Check submit button 
                if($this->input->post('submit')){
                    // if ($this->upload->do_upload('image_path')){
                    //get form's data and store in local varable
                    $title=$this->input->post('title');
                    $category=$this->input->post('category');
                    $ingredients=$this->input->post('ingredients');
                    //$image_path=$this->input->post('image_path');
                    $this->upload->do_upload('image_path'); 
                    $image_path= $this->upload->data();
                    $production_method=$this->input->post('production_method');
                    $production_time=$this->input->post('production_time');

                    $this->Recipe_model->form_insert([
                        'title' => $title,
                        'category_id' => $category,
                        'ingredients' => $ingredients,
                        'production_method' => $production_method,
                        'production_time' => $production_time,
                        'image_path' => $image_path['image_path']
                    ]);
                    redirect("Recipes/dispdata"); 
                }
            }

I tried to use Codeigniter documentation, but couldn’t work. I will be very greatful for every response, I am new in MVC. Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效