我做了一个codeigniter功能,所以当用户选择要上传的产品图片时,它必须以用户选择的形式显示为预览图片。这样,用户就可以看到他选择了哪张图片。
这是一个如何显示的链接:https://imgur.com/a/2b775
控制器中的索引函数:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
这是控制器代码中的上传功能:
public function upload() {
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name']= true;
$this->upload->initialize($config);
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
if(!$this->upload->do_upload('userfile')){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('product_form', $error);
}else{
//Main image
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
$config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 547;
$config['height'] = 430;
//Thumb image
$dataThumb = $this->upload->data();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = FALSE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this-> db-> insert('products', array(
'user_id' => $_SESSION['user_id'],
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
'product_naam' => $this->input->post('product_naam'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
'date_created' => date('Y-m-d'),
'date_updated' => date('Y-m-d')
));
$data['img'] = base_url().
'/upload/new_'.$data["raw_name"].$data['file_ext'];
$dataThumb['img'] = base_url().
'/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
header('location:https://kadokado-ferran10.c9users.io//Product/cadeaupagina_aangeboden');
}
}
这是product_foto行的视图表单文件:
<div class="image-upload">
<input id="file-input" type="file" name="userfile"/>
<h3>Upload foto </h3>
<label for="file-input">
<img src="../images/PLUS.jpg"/>
</label>
</div>
</td>
<?php if ($get_image) {
echo '<tr>';
echo '<td>'.img(array('width' => '120', 'height' => '80', 'src' => 'upload/'.$data->product_foto)).'</td>';
echo '<td> Bekijk | Verwijder </td>';
echo '</tr>';
}
else {
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
} ?>
</tr>
我得到的2个错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: controllers/Product.php
Line Number: 17
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 17
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
第二次错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_image
Filename: views/product_form.php
Line Number: 67
Backtrace:
File: /home/ubuntu/workspace/application/views/product_form.php
Line: 67
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 21
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
数据库信息和其他信息:
The map where the picture is stored: upload
The database table name: products
The product picture row is named: product_foto
答案 0 :(得分:2)
排除上传代码的任何可能问题,导致这些错误的原因是您要将$data
分配给类范围$this->data
而不是访问$this->data
,而只是$data
1}}在您的视图中调用。
修订代码:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? (!empty($products->result()) ? $products->result() : false ) : false;
$data = array('get_image' => $products);
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
作为旁注,我认为您不必访问$this->upload->data();
两次相同的数据。只需将其分配给类似$image
的变量。