【发布时间】:2017-06-20 13:38:33
【问题描述】:
美好的一天!我制作了一个表格,将产品图片上传到我的数据库并在另一个视图页面上显示该产品,但它并没有真正起作用。
这是我认为的表格:
<form action="<?php echo site_url('Product/product_form'); ?>" method="post">
<h2> Cadeau aanbieden</h2>
<table class="aanbieding-cadeau">
<tr>
<td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?></td>
</tr>
<tr>
<td><?php echo($selectField);?></td>
</tr>
<tr>
<td><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?></td>
</tr>
<tr>
<div class="checkbox">
<label><input type="checkbox" value="">Gebruik adres van mijn account</label>
</div>
<td>
<h4>Upload foto</h4>
<?php
echo $this->session->flashdata('msg');
$arr = array('name' => 'product_foto', 'type'=>'file');
echo form_open_multipart();
echo form_upload($arr);
echo form_submit('upload', 'Upload');
?>
</td>
</tr>
<tr>
<td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?></td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Cadeau aanbieden!</button>
</td>
</tr>
</table>
</form>
这是我认为无法上传图片的代码:
<?php
echo $this->session->flashdata('msg');
$arr = array('name' => 'product_foto', 'type'=>'file');
echo form_open_multipart();
echo form_upload($arr);
echo form_submit('upload', 'Upload');
?>
当我发送表单时,没有加载任何图片,也没有将图片添加到我的图像文件夹中。
这是我的产品控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
var $data = array();
public function index()
{
$this->load->view('product_form', $this->data);
$query = $this->db->query('SELECT * FROM products');
$image_id = $query->num_rows() + 1;
$config = array (
'upload_path' => './upload/',
'allowed_types' => 'jpg|jpeg|png|bmp',
'max_size' => 0,
'filename' => $image_id
);
$this->load->library('upload', $config);
if ($this->upload->do_upload('file')) {
$this->db->insert('products', array(
'product_foto' => $this->upload->file_name
));
$this->session->set_flashdata('msg', 'Success');
}
}
public function __construct()
{
parent::__construct();
$this->load->model('product_model');
$this->load->helper(array('form', 'url'));
}
public function product_form()
{
$save = array(
'product_naam' => $this->input->post('product_naam'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'product_categorie' => $this->input->post('product_categorie'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'product_foto' => $this->input->post('product_foto'),
'date_created' => date('Y-m-d'),
'date_updated' => date('Y-m-d')
);
$this->product_model->saveProduct($save);
redirect('https://kadokado-ferran10.c9users.io/AlleCadeausController');
}
}
注意:当我在常规表单之外使用此代码时,我可以将图片上传到我的数据库,并且图片确实会添加到我的文件夹中:
<?php
echo $this->session->flashdata('msg');
echo form_open_multipart();
echo form_upload('file');
echo form_submit('upload', 'Upload');
echo form_close();
?>
希望有人可以帮助我, 谢谢
【问题讨论】:
-
请设置您的普通表单 enctype="multipart/form-data" 如果它的工作..不确定
标签: php image codeigniter image-uploading codeigniter-3