codeigniter - 如何在 CodeIgniter 中上传图片?

标签 codeigniter file-upload

在 View 中

 <?php echo form_open_multipart('welcome/do_upload');?>
 <input type="file" name="userfile" size="20" />

在 Controller 中
function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['overwrite'] = TRUE;
    $config['encrypt_name'] = FALSE;
    $config['remove_spaces'] = TRUE;
    if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('userfile')) {
        echo 'error';
    } else {

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

我这样称呼这个函数
 $this->data['data'] = $this->do_upload();

并查看此图像:
<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>

我不知道有什么错误。

最佳答案

看来问题是您将表单请求发送到 welcome/do_upload ,并调用Welcome::do_upload() $this->do_upload() 的另一种方法.
因此,当您调用 $this->do_upload();在您的第二种方法 , $_FILES数组将是 .
这就是为什么var_dump($data['upload_data']);返回 NULL .
如果你想从 welcome/second_method 上传文件, 将表单请求发送至 欢迎/second_method 您在哪里调用 $this->do_upload(); .
然后更改表单辅助函数(在 View 内)如下1:

// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');

使用 CodeIgniter 上传文件
代码点火器 has documented the Uploading process 很好,通过使用文件上传库。
您可以查看用户指南中的示例代码;此外,为了更好地了解上传配置,请查看手册页末尾的配置项说明部分。
还有一些关于在 CodeIgniter 中上传文件的文章/示例,您可能需要考虑:
  • http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
  • http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
  • http://jamshidhashimi.com/image-upload-with-codeigniter-2/
  • http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
  • http://hashem.ir/CodeIgniter/libraries/file_uploading.html (CodeIgniter 3.0-dev 用户指南)

  • 就像 旁注:确保您已加载 urlform使用 CodeIgniter 示例代码之前的辅助函数:
    // Load the helper files within the Controller
    $this->load->helper('form');
    $this->load->helper('url');
    
    // Load the helper files within the application/config/autoload
    $autoload['helper'] = array('form', 'url');
    

    1.表格必须是“多部分”类型的文件上传。因此,您应该使用返回的 `form_open_multipart()` 辅助函数:
    ``

    关于codeigniter - 如何在 CodeIgniter 中上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17315962/

    相关文章:

    php - 为 codeIgniter ErrorException [8192] : mysql_real_escape_string() 设置数据库

    php - 在 php/mysql 中导入带有图像的 excel 文件

    php - 将多个文件上传到 amazon s3 失败

    java - 使用 Vaadin 14 中的 "Upload"小部件将文本文件的内容上传到内存中的字符串

    file-upload - 从浏览器将文件上传到另一个域的最佳方法是什么?

    c# - ASP.Net:写文件 block ..HTTP文件上传简历

    php - 将新语言名称插入到 codeigniter 中的数据库后,在语言文件夹中创建包含嵌套 php 文件的文件夹

    php - 为什么 codeigniter2 不以更安全的方式(例如 session )存储 csrf_hash?

    php - ExpressionEngine:单项模板

    jquery-mobile - Blueimp Jquery 文件上传插件未上传移动设备上的所有文件(在桌面上运行良好)