在方法中设置 $this->data 然后取消设置
我正在使用 codeigniter 上传图像,在方法中我正在执行以下操作,
if(isset($_FILES['product_image']['name'])) {
//some setup needed
$config['upload_path'] = "./media/images/products";
$config['allowed_types'] = "png|gif|jpg|jpeg";
$config['max_width'] = 1490;
$config['max_height'] = 400;
//make sure the library is running with clean config
$this->upload->initialize($config);
//do the upload
if(!$this->upload->do_upload('product_image')){
$this->data['image_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['image_data'] = $this->upload->data();
//die(print_r($this->data));
$this->template->build('/admin/products/create', $this->data);
}
}
所以基本上我检查 $_FILES 中是否有内容,然后上传是否分配 $this-data['image_data' ]
一路上传数据。但是,当我处理数据时,即将文件名保存在数据库中,我无法访问 $this->data['image_data']
下面是我尝试使用它的方式,
if($this->input->post("submit_create") == "Save") {
die(print_r($this->data['image_data']));
}
但是我得到出现以下错误,
消息:未定义索引:image_data
为什么我将事物分配给 $this 使它们不仅可以在整个方法中访问,而且可以在整个控制器中访问?
I am uploading an image with codeigniter, and in the method I am doing the following,
if(isset($_FILES['product_image']['name'])) {
//some setup needed
$config['upload_path'] = "./media/images/products";
$config['allowed_types'] = "png|gif|jpg|jpeg";
$config['max_width'] = 1490;
$config['max_height'] = 400;
//make sure the library is running with clean config
$this->upload->initialize($config);
//do the upload
if(!$this->upload->do_upload('product_image')){
$this->data['image_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['image_data'] = $this->upload->data();
//die(print_r($this->data));
$this->template->build('/admin/products/create', $this->data);
}
}
so basically I check to see if there is something in the $_FILES and then upload if there is assigning $this-data['image_data']
with the upload data along the way. However when I come to process the data i.e. save the filename in a database, I cannot access $this->data['image_data']
below is how I am trying to use it,
if($this->input->post("submit_create") == "Save") {
die(print_r($this->data['image_data']));
}
however I get the following error,
Message: Undefined index: image_data
why is this I though assigning things to $this made them accessible not just throughout the method but the entire controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,当您在示例中此时打印
image_data
时:您实际上应该使用
$image_data
来代替:I think, when you are printing
image_data
at this point in your example:You should actually be using
$image_data
instead: