在codeigniter

时间:2016-12-21 02:50:23

标签: php codeigniter

我已尝试过所有解决方案,但我不知道出了什么问题。 Codeigniter一直告诉我没有上传文件。我在项目的根目录下创建了该文件夹。我已经看到了其他类似的问题,但我无法让它与他们的解决方案一起工作。

这是我的控制者:

public function index() {
        $this->form_validation->set_rules('name', 'name', 'required|trim|max_length[45]');
        $this->form_validation->set_rules('type_id', 'type', 'required|trim|max_length[11]');
        $this->form_validation->set_rules('stock', 'stock', 'required|trim|is_numeric|max_length[11]');
        $this->form_validation->set_rules('price', 'price', 'required|trim|is_numeric');
        $this->form_validation->set_rules('code', 'code', 'required|trim|max_length[45]');
        $this->form_validation->set_rules('description', 'description', 'required|trim|max_length[45]');
        $this->form_validation->set_rules('active', 'active', 'required|trim|max_length[45]');
        $this->form_validation->set_rules('unit_id', 'unit', 'required|trim|max_length[45]');
        $this->form_validation->set_rules('userfile', 'File', 'trim');

        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');

        if ($this->form_validation->run() == FALSE) { // validation hasn't been passed
            $this->load->view('product/add_view');
        } else { // passed validation proceed to post success logic
            // build array for the model
            $form_data = array(
                'name' => set_value('name'),
                'type_id' => set_value('type_id'),
                'stock' => set_value('stock'),
                'price' => set_value('price'),
                'code' => set_value('code'),
                'description' => set_value('description'),
                'active' => set_value('active'),
                'unit_id' => set_value('unit_id')
            );

            $config = array(
                'upload_path' => "./uploads/",
                'allowed_types' => "gif|jpg|png|jpeg",
                'overwrite' => TRUE,
                'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                'max_height' => "768",
                'max_width' => "1024"
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config); //Make this line must be here.
            $imagen = set_value('userfile');
            // run insert model to write data to db

            if ($this->product_model->product_insert($form_data) == TRUE) { // the information has therefore been successfully saved in the db
                if (!$this->upload->do_upload($imagen)) {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('product/add_view', $error);
                } else {
                    $data = array('upload_data' => $this->upload->data());
                    $this->load->view('product/add_view', $data);
                }
            } else {
                redirect('products/AddProduct', 'refresh');
                // Or whatever error handling is necessary
            }
        }
}

这是我的观点(只显示重要的部分)

<?php // Change the css classes to suit your needs    

$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('products/AddProduct', $attributes); ?>

<p>
    <label for="picture">Picture <span class="required">*</span></label>
    <?php echo form_error('userfile'); ?>
<?php echo form_upload('userfile')?>
    <br/>
</p>

<p>
        <?php echo form_submit( 'submit', 'Submit'); ?>
</p>

<?php echo form_close(); ?>

编辑:根据答案应用修改我收到错误500.

1 个答案:

答案 0 :(得分:0)

我试着处理你的代码。这次改变后它对我有用了

$config = array(
    'upload_path' => "./uploads/",
    'upload_url' => base_url()."uploads/", //  base_url()."/uploads/", //added
    'allowed_types' => "gif|jpg|png|jpeg",
    'overwrite' => TRUE,
    'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
    'max_height' => "768",
    'max_width' => "1024"
);

$this->load->library('upload'); //changed
$this->upload->initialize($config); //Make this line must be here.
$imagen = userfile; // $_FILES['userfile']['name'] //changed

如果你得到localhost页面不起作用localhost当前无法处理这个请求。 HTTP ERROR 500可能会出现语法错误,我只需在代码末尾添加1个}。您可以查看phpinfo()以获取其他信息。此外,您的文件位置可能存在问题,可能是您指定的地址,也可能是您在服务器上运行此权限时的权限。