【发布时间】:2019-11-19 12:42:53
【问题描述】:
我正在尝试使用带有图像的产品信息构建项目。我尝试了下面给出的一些代码,但我无法在数据库中上传图像路径。我是一个尝试学习代码的新手。
控制器代码:-
class Category extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Category_model');
}
private function do_upload(){
$config['upload_path']='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('product_image')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/pages/add_product_form', $error);
}
else
{
$upload_data = $this->upload->data();
$this->Category_model->insert_data( $upload_data['upload_path']);
}
$data = array('upload_data' => $this->upload->data());
}
function save_product(){
$this->do_upload();
$this->session->set_userdata('message','Product saved successfully');
$this->add_product();
}
}
型号代码:-
function insert_data( $path_name){
$data = array(
'product_image' => $path_name
);
$data['product_status']=1;
$this->db->insert('tbl_product', $data);
}
查看代码:-
<form class="form-horizontal" action="<?php echo base_url()?>index.php/Category/save_product" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" for="typeahead">Product Name </label>
<div class="controls">
<input type="text" name="product_name" class="span6 typeahead" id="typeahead" data-provide="typeahead" data-items="4" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Product Category</label>
<div class="controls">
<select name="product_category" id="selectError3">
<option>Select Category</option>
<?php foreach($category_info as $category){ ?>
<option value="<?php echo $category->category_id ?>"><?php echo $category->category_name; ?></option>
<?php } ?>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="typeahead">Product Image </label>
<div class="controls">
<input name="product_image" type="file" name="fileToUpload" id="fileToUpload">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
这是我上面给出的代码,但仍然无法获取数据库中的图像路径,请在这方面提供帮助。在此先感谢。
【问题讨论】:
-
错误说明了什么问题,$upload_data 未定义,将
$this->Category_model->insert_data( $upload_data['upload_path']);移到else 子句中 -
@Vickel 感谢您的帮助。我已经编辑了代码,但仍然没有在数据库中获取图像路径。
-
在你的 else 子句中,
print_r($upload_data);die;输出什么? -
@Vickel 感谢您的回复,我更改了我的控制器代码(仅 else 子句),如下所示:- ``` else { $upload_data = $this->upload->data(); print_r($upload_data); $this->Category_model->insert_data($upload_data['upload_path']); } ``` 但是我没有得到任何关于 print_r($upload_data); 的输出
-
您的上传文件夹
uploads存在吗?它是否设置为可写(文件权限 chmod777)?
标签: php codeigniter image-uploading