2014-02-17 80 views
1

我有以下形式和控制器,它有一個圖像上傳,但一切順利,除非文件沒有被上傳到特定的文件夾。Codeigniter文件沒有得到上傳

查看

<?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'); 
?> 

它gaves我一個錯誤

A PHP Error was encountered 

Severity: Notice 

Message: Trying to get property of non-object 

Filename: controllers/addproduct_controller.php 

Line Number: 53 

請幫我這個或告訴我在哪裏我也做了錯誤。

回答

1

enctype屬性在你的標籤中缺失。

在你的表單標籤添加enctype="multipart/form-data"

OR

在CI,使用form_open_multipart函數生成表單標籤

按照討論意見,如下更新你的代碼。

$data  = array(
        'upload_data' => $this->upload->data() 
       ); 
$prod_img = $data["upload_data"]->file_name; 
+0

它給了我一個錯誤「試圖獲得非對象的屬性」當我加入它像負載>幫手(「形式」); ?> <?php echo form_open_multipart(base_url()。'index.php/addproduct_controller'); ?> –

+0

是的。你有什麼錯誤嗎? –

+0

是它說「嘗試獲取非對象的屬性」 –

0

你已經錯過了,包括表單屬性上傳文件

添加ENCTYPE = 「的multipart/form-data的」 在你的表單標籤

0

你必須讓html形式,但不加文件上傳標籤在您的表單創建。

在窗體標籤中添加:enctype="multipart/form-data"

<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post" enctype="multipart/form-data" >