【问题标题】:how to apply codeigniter file uploader in our project?如何在我们的项目中应用codeigniter文件上传?
【发布时间】:2015-05-04 03:15:40
【问题描述】:

我使用 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>

控制器 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";

}

}

}

点击按钮上传后浏览器显示上传失败,请指正,谢谢

【问题讨论】:

标签: codeigniter


【解决方案1】:

第一件事:注意到您在输入中缺少name="userfile"

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

第三:创建一个名为$input_name = "userfile";的变量,就在do上传的上方。然后在 do_upload 添加 $input_name like $this-&gt;upload-&gt;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);
        }
    }
}
?>

【讨论】:

  • 我工作过 wolfgang1983,现在出现一条新消息 上传路径似乎无效,之后
  • 路径似乎无效' ,之后我更改 $config['upload_path'] = './uploads/'; to $config['upload_path'] = base_url().'uploads/',我觉得进展不大但是一​​直没有成功
  • 你的上传文件夹在项目的主目录中吗?
  • 在这个地方“C:\xampp\htdocs\pasar\uploads”,所以我调用base_url()。制作动态位置。
  • 实际上我在更改此代码后得到了新的进展: $this->load->library('upload', $config);到 $this->upload->initialize($config);然后消息错误变成这样,“上传的文件超出了 PHP 配置文件中允许的最大大小”
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2018-06-08
相关资源
最近更新 更多