Codeigniter文件上传和数据库插入1表格

时间:2016-12-10 07:11:31

标签: php html codeigniter model-view-controller codeigniter-3

所以我试图在数据库中插入并上传文件。但是当我尝试提交时,它会返回服务器错误。

  

localhost页面无效

     

localhost目前无法处理此请求。

     

HTTP ERROR 500

我认为问题出现在$this->upload->do_upload('userfile'),因为我试图从这部分中注释掉并显示回显配置数组并且没有错误但是当我试图取消注释到这部分时,错误显示。

控制器

public function add_now(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');

    if($this->form_validation->run() == FALSE){
        $this->add();
    }
    else{   
        $query = $this->events_model->do_upload();
        if($query){
            $this->session->set_flashdata('success', 'Successful!');
            $this->index();
        }
        else{
            if(!$this->session->flashdata('upload_error')){
                $this->session->set_flashdata('failed', 'Failed!');
            }
            $this->add();
        }
    }
}

模型

public function do_upload(){

    $config['upload_path'] = './resources/images/events_photo/temp/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['file_name'] = uniqid().'.jpeg';
    $config['overwrite'] = TRUE;


    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('userfile')){
        $this->session->set_flashdata('upload_error', $this->upload->display_errors());
    }
}

查看

<?php echo form_open_multipart('events/add_now'); ?>
                <div class="panel-body">
                    <div class="row">
                        <div class="form-group">
                            <label class="control-label">Name</label>
                            <input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
                            <small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label">Start Date</label>
                                <input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
                                <small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
                            </div>
                        </div>

                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label">End Date</label>
                                <input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
                                <small class="text-danger"><?php echo form_error('Event_End'); ?></small>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-9">
                            <div class="form-group">
                                <label class="control-label">Location</label>
                                <input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
                                <small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
                    </div>
                    <div class="row">
                        <div class="form-group">
                                <label class="control-label">Image Attachment (Optional)</label>
                                <input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
                                <?php if($this->session->flashdata('upload_error')): ?>
                                <small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
                            <?php endif; ?>
                            </div>
                    </div>
                </div>
                <div class="panel-footer text-right">
                    <button class="btn btn-success" type="submit">Submit</button>
                </div>
            </form>

1 个答案:

答案 0 :(得分:0)

首先在控制器中加载url帮助器...

$this->load->helper('url');

然后将上传文件路径设置为:

$config['upload_path'] = base_url('resources/images/events_photo/temp/');

尝试它可能有效。

那么这两个功能呢?

$this->add();

$this->success();