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

时间:2018-10-26 18:36:15

标签: php codeigniter

我正在用PHP Codeigniter创建食谱书。我在将图像上传到资源文件夹并将图像路径保存到dtb时遇到问题。除文件(图像)外,所有数据均成功保存到数据库。 查看。

 <?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(); ?>

控制器

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"); 
                }
            }

我尝试使用Codeigniter文档,但无法使用。我会非常感激每一个回应,我是MVC的新手。谢谢。

0 个答案:

没有答案