2014-08-27 19 views
1

嘿傢伙我在codeigniter的上傳圖像函數中遇到了問題..我爲我的上傳函數創建了一個新的控制器,因爲當我將上傳函數放入我的主控制器..所以這裏是upload.php的,我把如何在codeigniter中添加新的控制器

<?php 

class Upload extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
    } 



    public function index() 
    { 
     $this->load->view('account', array('error' => ' ')); 
    } 

    public function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 


     $this->load->library('upload', $config); 

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error'=>$this->upload->display_errors()); 
      $this->load->view('account', $error); 
     } 
     else 
     { 
      $data=array('upload_data'=>$this->upload->data()); 
      $data['img'] = base_url().'/uploads/'; 
      $this->load->view('success', $data); 
     } 
    } 
} 
?> 

在這裏,新的控制器是用戶可以上傳自己的圖像account.php

<html> 
<body>  
<?php echo form_open_multipart('upload/do_upload');?> 

    <input type="file" name="userfile" /> 

    <br /><br /> 

    <input type="submit" value="upload" /> 
</body> 
</html> 

,我也宣佈新的控制器在我的路線

$default_controller = "admin"; 
$route['verifylogin'] = 'verifylogin'; 
$route['home'] = 'home'; 
$route['upload'] = 'upload'; 
$controller_exceptions = array('somethingpink','forums'); 

$route['default_controller'] = $default_controller; 
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1'; 
$route['404_override'] = ''; 

,問題是,每次我的圖片上傳我不斷收到「404頁找不到」錯誤..

它不能找到這個URL爲http://本地主機/ picturecity/index.php文件/ upload/do_upload .. picturecity是我的項目的名稱..該url是由這個form_open_multipart('upload/do_upload');自動生成的?> ..我不知道有什麼問題..是它的路由還是出現故障的控制器..請幫助我..我要通過我的項目明天..

我的.htaccess

RewriteEngine On 
RewriteBase /picturecity 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

回答

0

請試試這個。並確保您的上傳文件夾具有777權限。

public function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['overwrite'] = 'TRUE'; 

     $this->load->library('upload'); 
     $this->upload->initialize($config); 

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error'=>$this->upload->display_errors()); 
      $this->load->view('account', $error); 
     } 
     else 
     { 
      $data=array('upload_data'=>$this->upload->data()); 
      $data['img'] = base_url().'/uploads/'; 
      $this->load->view('success', $data); 
     } 
    } 
+0

我試過你的代碼,但它仍然顯示404頁面沒有找到當我上傳圖片..我認爲我的問題是在鏈接..但我解決不了..我花了幾個小時找到一個但我真的不能 – James 2014-08-27 19:21:39

+0

訪問您的上傳文件夾的權限。意味着它應該有權寫入和讀取兩者。 777代表完全訪問權限。 – Naincy 2014-08-27 19:25:06

+0

或者如果你分享你的.htaccess那麼它也會很好 – Naincy 2014-08-27 19:27:06

相關問題