图像未上传到图像目录,但存储名称在数据库中

时间:2016-06-02 09:44:42

标签: php codeigniter

控制器代码:

public function create(){

    $row = $this->model->set_attribute( ( isset( $_POST["model"] ) )?$_POST["model"]:array() );

    if( !empty( $_POST["model"] ) ){


        $rand = rand( 10000000, 9999999 ). '_';
        if( !empty( $_FILES['model']['name']['image'] ) ){
            move_uploaded_file( $_FILES['model']['tmp_name']['image'], FCPATH. '/assets/uploads/'. $rand. $_FILES['model']['name']['image'] );
            $_POST['model']['image'] = $rand. $_FILES['model']['name']['image'];
        }
        $id = $this->model->insert( $_POST["model"] );
        if( $id != null ){
            $row = $this->model->get( $id );
            app::set_flash( "success", "Record is added." );
            $this->_redirect( "index" );
        }
    }
    $this->_render( "form", array(
        "model" => $this->model,
        "page_heading" => $this->page_heading,
        "row" => $row,
    ) );
}
views
<div class="form-group">
            <label for="image"><?php echo( $model->attributeLabels( 'image' ) ); ?> <small class="danger highlight "><?php echo form_error( 'image' ) ?></small></label>
            <input type="file" class="form-control" name="model[image]" id="main_image" placeholder="<?php echo( $model->attributeLabels( 'image' ) ); ?>" value="<?php echo $row->image; ?>" />
            <?php echo( $row->image ); ?>
        </div>

在上面的控制器功能中将图像名称存储在数据库中但不上传图像目录资产/上传中的图像

1 个答案:

答案 0 :(得分:0)

它应该在表格中遗漏属性enctype =“multipart / form-data”