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

时间:2022-10-06 12:47:38

I make simple file uploader with codeigniter and then as my espect the program running well , when I try to apply in my project. method do_upload is executed but file can't uploaded. this is my code

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

Controller path C:\xampp\htdocs\pasar\application\modules\crud

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

Upload path C:\xampp\htdocs\pasar\uploads

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

view 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";

}

}

}

After button upload clicked then the browser display upload failed, please give me corection, thanx

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

1 个解决方案

#1


First: thing noticed your missing is name="userfile" in input.

首先:注意到你输入的东西是name =“userfile”。

Second: on your controller you do not have a view in index()

第二:在你的控制器上你没有index()中的视图

Third: create a variable called $input_name = "userfile"; Just above the do upload. And then in the do_upload add $input_name like $this->upload->do_upload($input_name)

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

Fourth: thing noticed

第四:注意到的事情

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

Change to

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

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

Codeigniter Docs http://www.codeigniter.com/docs/

Codeigniter Docs http://www.codeigniter.com/docs/

Userguide: http://www.codeigniter.com/userguide2/libraries/file_uploading.html

This is single file upload only.

这只是单个文件上传。

View

<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>

Controller

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

#1


First: thing noticed your missing is name="userfile" in input.

首先:注意到你输入的东西是name =“userfile”。

Second: on your controller you do not have a view in index()

第二:在你的控制器上你没有index()中的视图

Third: create a variable called $input_name = "userfile"; Just above the do upload. And then in the do_upload add $input_name like $this->upload->do_upload($input_name)

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

Fourth: thing noticed

第四:注意到的事情

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

Change to

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

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

Codeigniter Docs http://www.codeigniter.com/docs/

Codeigniter Docs http://www.codeigniter.com/docs/

Userguide: http://www.codeigniter.com/userguide2/libraries/file_uploading.html

This is single file upload only.

这只是单个文件上传。

View

<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>

Controller

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