将图像上传到项目目录,并使用Codeigniter将其他数据存储在数据库中

时间:2015-12-13 07:24:39

标签: php codeigniter

我正在使用表单在项目文件夹中上传图像并将其他数据存储到数据库中。当我尝试提交此表单时,数据已提交且图像未上传,并且还有一个图像已上传,并且并非所有数据都存储在数据库中,这是我的代码

模型:

function update_news($data) {

    extract($data);
    $this->db->where('news_id', $news_id);
    $this->db->update($table_name, array('title_en' => $title_en, 'title_ar' => $title_ar,'news_date' => $news_date,'image' => $image,'is_visible' => $is_visible,'main_news' => $main_news));
    return true;
}

控制器

public function update() {
    $filename = $this->input->post('image');
    $date = DateTime::createFromFormat("Y-m-d", $this->input->post('news_date'));
    $data = array(
        'table_name' => 'news', // pass the real table name
        'news_id' => $this->input->post('news_id'),
        'title_en' => $this->input->post('title_en'),
        'title_ar' => $this->input->post('title_ar'),
        'news_date' => $this->input->post('news_date'),
        'image' => $date->format("Y") . '/' . $this->input->post('image'),
        'is_visible' => $this->input->post('is_visible'),
        'main_news' => $this->input->post('main_news')
    );

    $this->m_news_crud->update_news($data);

    $folderName = 'assets/images/press-news/2015';
    $config['upload_path'] = "$folderName";
    if (!is_dir($folderName)) {
        mkdir($folderName, 0777, TRUE);
    }

    $config['file_name'] = $this->input->post('image');
    $config['overwrite'] = TRUE;        
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 100;
    $config['max_width'] = 1024;
    $config['max_height'] = 768;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if (!$this->upload->do_upload('userfile','image')) {
        $error = array('error' => $this->upload->display_errors());
        $data['error'] = $error;
        $data['title_en'] = $this->input->post('title_en');
        $data['title_ar'] = $this->input->post('title_ar');
        $data['news_date'] = $this->input->post('news_date');
        $data['image'] = base_url('assets/images/press-news/' . $date->format("Y")) . '/' . $this->input->post('image');
        $data['is_visible'] = $this->input->post('is_visible');
        $data['main_news'] = $this->input->post('main_news');
        $data['news_id'] = $this->input->post('news_id');
        $this->load->view('admin/news/d_admin_header');
        $this->load->view('admin/news/vcrudedit_news', $data);
        $this->load->view('admin/news/d_admin_footer');
    } else {
        $data = array('upload_data' => $this->upload->data());

        redirect('c_news_crud/get_news_data', $data);

        echo $filename;
    }
}

视图:

<div class="container">
<div class="col-md-9">
    <?php echo form_open('C_news_crud/update', 'role="form"  style="margin-top: 50px;"'); ?>
    <div class="form-group">
        <label for="title_en">Title Name English</label>
        <input type="text" class="form-control" id="title_en" name="title_en" value="<?php echo $title_en ?>">
    </div>
    <div class="form-group">
        <label for="title_ar">Title Name Arabic</label>
        <input type="text" class="form-control" id="title_ar" name="title_ar" value="<?php echo $title_ar ?>">
    </div>
    <div class="form-group">
        <div class="col-md-4 col-md-offset-4">
            <label for="is_visible">is visible</label>
            <?php
            if ($is_visible == 1) {
                $check_visible = 'checked="checked"';
            } else {
                $check_visible = ' ';
            }
            ?>

            <input role="checkbox" type="checkbox" id="is_visible" class="cbox" <?php echo $check_visible; ?> name="is_visible" value="<?php echo $is_visible; ?>">
        </div>
        <div class="col-md-4">
            <label for="main_news">Main Arabic</label>
            <?php
            if ($main_news == 1) {
                $check_main = 'checked="checked"';
            } else {
                $check_main = ' ';
            }
            ?>

            <input role="checkbox" type="checkbox" id="main_news" class="cbox" <?php echo $check_main; ?> name="main_news" value="<?php echo $main_news; ?>">
        </div>
    </div>
    <div class="form-group">
        <label for="news_date">News date</label>
        <input type="date" class="form-control" id="news_date" name="news_date" value="<?php echo $news_date ?>">
    </div>
    <div class="form-group">
        <label for="image">image</label>
        <input type="file" class="form-control" id="image" name="image" value="<?php echo $image ?>">
    </div>
    <input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
    <input type="submit" name="save" class="btn btn-primary" value="Update">
    <button type="button" onclick="location.href = '<?php echo site_url('C_news_crud/get_news_data') ?>'" class="btn btn-success">Back</button>
    </form>
    <?php echo form_close(); ?>
</div>

1 个答案:

答案 0 :(得分:0)

  1. 确保上传文件夹的路径正确且具有写入权限。
  2. 确保您指的是正确的文件输入名称:

    $config['file_name'] = $_FILES['image']['name'];
    
  3. 将您的上传目录设置为./assets/images/press-news/2015/

  4. 您需要显示您收到的错误,以便了解导致错误的原因!

    if ( ! $this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    
  5. syscalls(2)