简体   繁体   English

使用CodeIgniter上传文件失败

[英]File uploading using CodeIgniter is failing

Title says is all. 标题说的就是全部。

Form in view: 视图形式:

    <tr>
        <form action="<?=base_url()?>index.php/admin/product_add" method="post" enctype="multipart/form-data">
            <td><input type="text" name="pid"></td>
            <td><input type="text" name="name"></td>
            <td><input type="file" name="image" id = "image"></td>
            <td><input type="text" name="price"></td>
            <td><textarea name="description"></textarea></td>
            <td><input type="text" name="type"></td>
            <td>
                <input type="submit" value="add">
            </td>
        </form>
    </tr>

Function used in controller: 控制器中使用的功能:

            public function product_add()
    {
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
        /*upload stuff*/
        $config = array();
        $config['upload_path'] = base_url()."imgs/products/";
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['max_size'] = '9999';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        $_POST['price']=str_replace(",", ".", $_POST['price']);
        $this->form_validation->set_rules('pid','Product ID','required');
        $this->form_validation->set_rules('name','Denumire Produs','required');
        $this->form_validation->set_rules('image','Imagine','required');
        $this->form_validation->set_rules('price','Pret','required');
        $this->form_validation->set_rules('description','Descriere','required');
        $this->form_validation->set_rules('type','Tip','required');

        if ( ! $this->upload->do_upload('image'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('admin/produse', $error);
            echo "epic fail";
        }
        else
        {
            if ($this->form_validation->run() == FALSE)
            {
                echo "FAIL";    
            }
            else
            {
                echo "asdsad";
                $this->db_actions->addProduct($_POST["pid"],$_POST["name"],$_POST["image"],$_POST["price"],$_POST["description"],$_POST["type"]);
                redirect(base_url()."index.php/admin/produse");
            }
        }
    }

I've been busting my ass with this all day so I'm seriously frustrated atm. 我整天都在捣蛋,所以我对atm感到非常沮丧。

I'm getting the error because if ( ! $this->upload->do_upload('image')) is indeed false. 我收到错误消息是因为if(!$ this-> upload-> do_upload('image'))确实为假。 Every.Single.Damn.Time. 每个单身的时间

What am I doing wrong here ? 我在这里做错了什么?

Is the error message not giving you anything helpful? 错误消息没有给您任何帮助吗? It looks like what Kai Qing mentioned, you need to use the server (file system) path as per EllisLab's File Upload Docs and not the URL path. 看起来像Kai Qing提到的那样,您需要按照EllisLab的文件上传文档使用服务器(文件系统)路径,而不是URL路径。

here some of my codes of uploading files 这是我上传文件的一些代码

function upload(){ 函数upload(){

            $config['upload_path']   = './files/contracts-csv/'; 
            $config['allowed_types'] = 'csv';   
            $config['max_size']      = '4096';      
            $config['overwrite']     =  TRUE;

            $this->load->library('upload', $config);
            $this->upload->display_errors('', '');

            if (!$this->upload->do_upload("image")) {
                  echo $this->upload->display_errors(); die();
                  $this->data['error'] = array('error' => $this->upload->display_errors());
                 //error message
            } else {
                //do something
            }
            redirect("contracts/upload_exit");  
    }
$config['upload_path'] = "var/www/your_app_images_path";
$config['allowed_types'] = "gif|jpg|png|jpeg";
$config['overwrite'] = false;
$config['remove_spaces'] = true;
$this->load->library('upload', $config);

Check your upload_path it should be like above. 检查您的upload_path,它应该像上面一样。

if (!$this->upload->do_upload("file_fieldName")) {                        
    pr($this->upload->display_errors());exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM