Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以codeigniter one格式上载图像并将数据保存到数据库中_Php_Forms_Codeigniter_File Upload - Fatal编程技术网

Php 以codeigniter one格式上载图像并将数据保存到数据库中

Php 以codeigniter one格式上载图像并将数据保存到数据库中,php,forms,codeigniter,file-upload,Php,Forms,Codeigniter,File Upload,我使用的是codeigniter mvc框架,我想制作一个表单,它有一个上传图片,还可以将输入的数据以输入类型保存在一个html中。为什么这是我的图像无法上传,当我回显错误时,它会显示如下错误: File cannot be uploadedarray(1) { ["error"]=> string(43) " You did not select a file to upload. " } 以下是我的代码: <form id="frm_product" class="form-

我使用的是codeigniter mvc框架,我想制作一个表单,它有一个上传图片,还可以将输入的数据以输入类型保存在一个html
中。为什么这是我的图像无法上传,当我回显错误时,它会显示如下错误:

File cannot be uploadedarray(1) { ["error"]=> string(43) "
You did not select a file to upload.

" }
以下是我的代码:

<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct">  
                <div style="position:relative;">
                    <a class='btn btn-primary' href='javascript:;'>
                        Choose Picture..
                        <input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40"  onchange='$("#upload-file-info").html($(this).val());'>
                    </a>
                    &nbsp;
                    <span class='label label-info' id="upload-file-info"></span>
                </div>
                <br>
                <div class="form-group">
                    <label for="cat_name">Product Name: </label>
                    <input type="text" class="form-control" name="prod_name">
                </div>
                <div class="form-group">
                    <label for="cat_desc">Description: </label>
                    <input type="text" class="form-control" name="prod_desc">
                </div>
                <div class="form-group">
                    <label for="cat_desc">Price: </label>
                    <input type="text" class="form-control" name="prod_price">
                </div>
                <div class="form-group">
                    <label for="cat_desc">Category: </label>
                    <select class="form-control" name="prod_cat">
                      <?php foreach($data as $each){ ?>
                        <option value="<?php echo $each->Category_id; ?>"><?php echo $each->Category_name; ?></option>';
                      <?php } ?>  
                    </select>
                </div>
            </form>    
在我的控制器中,这是我的代码:

public function addProduct(){
        $config['upload_path'] = './assets/uploaded_images/';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['max_size'] = 1024 * 8;
        $this->load->library('upload', $config);
        if($this->upload->do_upload("file_source")) {
            $upload_data = $this->upload->data(); 
            $file_name =   base_url().'assets/uploaded_images/'.$upload_data['file_name'];
            $data = array (
            'Product_name' => $this->input->post('prod_name'),
            'Product_desc' => $this->input->post('prod_desc'),
            'Product_price' => $this->input->post('prod_price'),
            'Category_id' => $this->input->post('prod_cat'),
            'Product_img' => $file_name
            );
            var_dump($data);    

        }
        else {
            echo "File cannot be uploaded";
            $error = array('error' => $this->upload->display_errors()); var_dump($error);
        }

    }

在我看来,您似乎忘记将
enctype=“multipart/form data”
添加到表单中


您如何提交此表单???@knizer opss我忘记了javascript我将编辑我的帖子,您将在其中调用此函数(submitProduct())?您可以使用codeigniter表单助手
表单打开('Admin\u控件/addProduct')
这对文件非常有用
表单打开('Admin\u控件/addProduct'))
此处解释和@wolfgang1983 thaks这太棒了我应该在哪里添加它?请帮忙
public function addProduct(){
        $config['upload_path'] = './assets/uploaded_images/';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['max_size'] = 1024 * 8;
        $this->load->library('upload', $config);
        if($this->upload->do_upload("file_source")) {
            $upload_data = $this->upload->data(); 
            $file_name =   base_url().'assets/uploaded_images/'.$upload_data['file_name'];
            $data = array (
            'Product_name' => $this->input->post('prod_name'),
            'Product_desc' => $this->input->post('prod_desc'),
            'Product_price' => $this->input->post('prod_price'),
            'Category_id' => $this->input->post('prod_cat'),
            'Product_img' => $file_name
            );
            var_dump($data);    

        }
        else {
            echo "File cannot be uploaded";
            $error = array('error' => $this->upload->display_errors()); var_dump($error);
        }

    }