我有以下表单和控制器,它有图像上传,但一切顺利,除了文件没有上传到特定文件夹。
查看
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce cms - home page</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post">
<?php
include 'header-adminpanel.php';
?>
<div class="container">
<div class="body-content">
<div class="side-left"><?php
include 'adminproduct_sidebar.php';
?></div>
<div class="side-right">
<br />
<table>
<tr>
<td class="captions">Product Code</td>
<td><input name="txt_pcode" type="text"/></td>
</tr>
<tr>
<td class="captions">Product Name</td>
<td><input name="txt_pname" type="text" size="40" /></td>
</tr>
<tr>
<td class="captions">Product Price</td>
<td><input name="txt_pprice" type="text" /></td>
</tr>
<tr>
<td class="captions">Product Description</td>
<td><textarea name="txt_pdesc" style="width:300px;height:100px;"></textarea></td>
</tr>
<tr>
<td class="captions">Product Image</td>
<td><input type="file" name="userfile" size="20" /></td>
</tr>
<tr>
<td class="captions">Product Options</td>
<td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
</tr>
<tr><td><input name="btn_add" class="button" type="submit" value="Add" /></td></tr>
</table>
<br />
</div>
</div>
</div>
<div style="clear:both"></div>
<?php
include 'footer.php';
?>
</form>
</body>
</html>
控制器
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Addproduct_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
if (isset($_POST["btn_logout"])) {
$this->session->sess_destroy();
$this->load->view('welcome_view');
} else if (isset($_POST["btn_home"])) {
$this->load->view('welcome_view');
} else if (isset($_POST["btn_account"])) {
} else if (isset($_POST["btn_add"])) {
$prod_img = 'no image';
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
// $error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
//return 'error';
} else {
global $prod_img;
$data = array(
'upload_data' => $this->upload->data()
);
$prod_img = $data->file_name;
// $this->load->view('upload_success', $data);
}
$prod_name = $_POST["txt_pname"];
$prod_code = $_POST["txt_pcode"];
$prod_price = $_POST["txt_pprice"];
$prod_desc = $_POST["txt_pdesc"];
$prod_options = $_POST["txt_poptions"];
$this->load->model('product_model');
$addproduct_result = $this->product_model->addProduct($prod_code, $prod_name, $prod_price, $prod_desc, $prod_img);
if ($addproduct_result == true) {
echo "Added Successfully";
} else {
echo "Failed";
}
}
}
}
然后我尝试添加以下代替普通标签。
<?php
$this->load->helper('form');
?>
<?php
echo form_open_multipart(base_url().'index.php/addproduct_controller');
?>
它给我一个错误
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/addproduct_controller.php
Line Number: 53
请帮我解决这个问题,或者告诉我我犯了哪些错误。
答案 0 :(得分:1)
enctype
属性。
在表单标记中添加enctype="multipart/form-data"
或强>
在CI中,使用form_open_multipart
函数生成表单标记
根据评论中的讨论,请按以下方式更新您的代码。
$data = array(
'upload_data' => $this->upload->data()
);
$prod_img = $data["upload_data"]->file_name;
答案 1 :(得分:0)
您错过了包含表单属性以上传文件
添加enctype =&#34; multipart / form-data&#34;在您的表单标签
答案 2 :(得分:0)
您已在表单创建中创建Fragment duedateFrag = new FieldVisitFragment();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.tabcontent, duedateFrag);
ft.commit();
表单但未添加文件上传标记。
在表单标记中添加html
。
enctype="multipart/form-data"