问题描述
查看
<?php echo form_open_multipart('welcome / do_upload');?>
< input type =filename =userfilesize =20/>
在控制器中
code> 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
,并调用欢迎::
方法。 $ this-> do_upload()
在另一个中的欢迎:: do_upload()
因此,当您在第二种方法中调用 $ this-> do_upload();
时, $ _ FILES
数组
这就是为什么 var_dump ['upload_data']);
返回 NULL
。
来自 welcome / second_method
的文件,将表单请求发送到 welcome / second_method ,其中调用 $ this->然后按如下所示更改表单助手函数(在 View 中)::
//将'second_method'更改为您的方法名
echo form_open_multipart / second_method');
使用CodeIgniter上传档案
CodeIgniter 很好,通过使用文件上传库。
您可以查看用户指南中的示例代码;此外,为了更好地了解上传配置,请检查手册页末尾的配置项说明部分。
还有几个有关在CodeIgniter中上传文件的文章/示例,您可能需要考虑:
- 旁注: 在使用CodeIgniter示例代码之前,请确保已加载
url
和形式
帮助函数://在控制器中加载帮助文件
$ this-> load-> helper('form');
$ this-> load-> helper('url');
In view
<?php echo form_open_multipart('welcome/do_upload');?> <input type="file" name="userfile" size="20" />
In controler
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()); } }
And I call this function like this
$this->data['data'] = $this->do_upload();
and view this image:
<ul> <?php foreach ($data['upload_data'] as $item => $value):?> <li><?php echo $item;?>: <?php echo $value;?></li> <?php endforeach; ?> </ul>
I don't know what's the error.
解决方案It seems the problem is you send the form request to
welcome/do_upload
, and call theWelcome::do_upload()
method in another one by$this->do_upload()
.Hence when you call the
$this->do_upload();
within your second method, the$_FILES
array would be empty.And that's why
var_dump($data['upload_data']);
returnsNULL
.If you want to upload the file from
welcome/second_method
, send the form request to the welcome/second_method where you call$this->do_upload();
.Then change the form helper function (within the View) as follows:
// Change the 'second_method' to your method name echo form_open_multipart('welcome/second_method');
File Uploading with CodeIgniter
CodeIgniter has documented the Uploading process very well, by using the File Uploading library.
You could take a look at the sample code in the user guide; And also, in order to get a better understanding of the uploading configs, Check the Config items Explanation section at the end of the manual page.
Also there are couple of articles/samples about the file uploading in CodeIgniter, you might want to consider:
- 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/2011/06/14/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 User Guide)
Just as a side-note: Make sure that you've loaded the
url
andform
helper functions before using the CodeIgniter sample code:// Load the helper files within the Controller $this->load->helper('form'); $this->load->helper('url');
这篇关于如何在CodeIgniter中上传图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!