在 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 中上传文件的文章/示例,您可能需要考虑:
就像 旁注:确保您已加载
url
和 form
使用 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/