如何在我们的项目中应用codeigniter文件上传器?

时间:2015-05-04 03:15:41

标签: codeigniter

我使用codeigniter创建简单的文件上传器,然后当我尝试在我的项目中应用时,我认为程序运行良好。方法do_upload已执行但文件无法上传。这是我的代码

控制器路径 C:\xampp\htdocs\pasar\application\modules\crud

上传路径 C:\xampp\htdocs\pasar\uploads

查看v_foto.php

<form method="post" action="<?php echo base_url().'index.php/crud/c_foto/do_upload' ?>" enctype="multipart/form-data" >
<h3>Upload Foto </h3>
<input type="file">
<input type="submit" value="upload" />
</form>

controller c_upload.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_foto extends MY_Controller {

function __construct() {

$this->load->helper(array('form', 'url'));

}

public function index() {

}

function do_upload(){

//preferences

$config['upload_path'] ='.uploads/';

$config['allowed_types'] = 'gif|jpg|png';

$config['max_size'] = '100';

$config['max_width']  = '1024';

$config['max_height']  = '768';

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

if ( ! $this->upload->do_upload()){

    $error = array('error' => $this->upload->display_errors());

    echo "Upload failed";

} else {

    $data = array('upload_data' => $this->upload->data());

    echo "Upload sukses";

}

}

}

点击按钮上传后,浏览器显示上传失败,请给我核心,谢谢

1 个答案:

答案 0 :(得分:0)

首先:注意到您输入的内容是name="userfile"

你的控制器上的

第二:你没有index()

中的视图

第三:创建一个名为$input_name = "userfile";的变量,就在do upload上方。然后在 do_upload 中添加$input_name,如$this->upload->do_upload($input_name)

第四:注意事项

$config['upload_path'] ='.uploads/';

更改为

$config['upload_path'] = './uploads/';

Codeigniter文档http://www.codeigniter.com/docs/

用户指南:http://www.codeigniter.com/userguide2/libraries/file_uploading.html

这只是单个文件上传。

查看

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('index.php/crud/c_foto/do_upload');?>

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

<br /><br />

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

</form>

</body>
</html>

控制器

<?php

// MX HMVC 
// CI Codeigniter
class C_foto extends MX_Controller {

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

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

    function do_upload()
    {

        /*
             Main Directory: 

             application
             uploads
             system
             index.php

        */

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0'; // No Limit
        $config['max_width']  = '0'; // No Limit
        $config['max_height']  = '0'; // No Limit

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

        // Set your input name from <input type="file" name="userfile" size="20" />

        $input_name = "userfile";

        if ( ! $this->upload->do_upload($input_name))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }
}
?>