Title说的就是全部。
视图中的表单:
<tr>
<form action="<?=base_url()?>index.php/admin/product_add" method="post" enctype="multipart/form-data">
<td><input type="text" name="pid"></td>
<td><input type="text" name="name"></td>
<td><input type="file" name="image" id = "image"></td>
<td><input type="text" name="price"></td>
<td><textarea name="description"></textarea></td>
<td><input type="text" name="type"></td>
<td>
<input type="submit" value="add">
</td>
</form>
</tr>
控制器中使用的功能:
public function product_add()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
/*upload stuff*/
$config = array();
$config['upload_path'] = base_url()."imgs/products/";
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '9999';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$_POST['price']=str_replace(",", ".", $_POST['price']);
$this->form_validation->set_rules('pid','Product ID','required');
$this->form_validation->set_rules('name','Denumire Produs','required');
$this->form_validation->set_rules('image','Imagine','required');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('description','Descriere','required');
$this->form_validation->set_rules('type','Tip','required');
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/produse', $error);
echo "epic fail";
}
else
{
if ($this->form_validation->run() == FALSE)
{
echo "FAIL";
}
else
{
echo "asdsad";
$this->db_actions->addProduct($_POST["pid"],$_POST["name"],$_POST["image"],$_POST["price"],$_POST["description"],$_POST["type"]);
redirect(base_url()."index.php/admin/produse");
}
}
}
我一整天都在忙这个,所以我对atm非常沮丧。
我得到错误是因为if(!$this->upload->do_upload('image'))确实是假的。Every.Single.Dam.Time.
我在这里做错了什么?
错误消息对您没有任何帮助吗?看起来就像Kai Qing提到的,你需要按照EllisLab的文件上传文档使用服务器(文件系统)路径,而不是URL路径。
这里是我上传文件的一些代码
函数上传(){
$config['upload_path'] = './files/contracts-csv/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '4096';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->display_errors('', '');
if (!$this->upload->do_upload("image")) {
echo $this->upload->display_errors(); die();
$this->data['error'] = array('error' => $this->upload->display_errors());
//error message
} else {
//do something
}
redirect("contracts/upload_exit");
}
$config['upload_path'] = "var/www/your_app_images_path";
$config['allowed_types'] = "gif|jpg|png|jpeg";
$config['overwrite'] = false;
$config['remove_spaces'] = true;
$this->load->library('upload', $config);
检查你的上传路径,它应该是上面的样子。
if (!$this->upload->do_upload("file_fieldName")) {
pr($this->upload->display_errors());exit;
}