如何在codeigniter中添加新的控制器

时间:2014-08-27 19:10:36

标签: php codeigniter

嘿伙计我在codeigniter上的上传图片功能有问题。我为我的上传功能创建了一个新的控制器,因为当我将上传功能放在我的主控制器中时我一直收到错误..所以这里是upload.php我做的新控制器

<?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:// localhost / picturecity / index.php / upload / do_upload .. picturecity是我的项目的名称..这个url是由这个form_open_multipart('upload / do_upload')自动生成的; ?&GT; ..我不知道是什么问题..是路由还是失败的控制器..请帮助我..明天我需要通过我的项目..

我的HTACCESS

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

1 个答案:

答案 0 :(得分: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);
        }
    }