今天花了不少时间弄CI文件上传,把我遇到的几个问题及解决方法和大家分享下:
上传文件大小问题
1.Warning: POST Content-Length of 4970832 bytes exceeds the limit of 2097152 bytes inUnknown on line 0
解决:设置php.ini post_max_size=10M
2.The uploaded file exceeds the maximum allowed size in your PHP configuration file.
解决:php.in upload_max_filesize=10M
3.The file you are attempting to upload is larger than the permitted size.
$config['max_size'] = 1024*1024*10; //大小10M
You need to set the value of upload_max_filesize
and post_max_size
in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
路径问题:刚开始我的upload配置
$config['upload_path'] =base_url().'upload/';//根目录的upload文件
$config['allowed_types'] = 'txt|php|cdr|gif|jpg|png';
$config['overwrite']=true;
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
报错
error:The upload path does not appear to be valid.
显然 'upload_path'没有找到,网上查了下
$config['upload_path'] =base_url().'upload/';这里base_url().'upload/';是服务器路径应该要换成绝对路径:getcwd().'./upload/';测试了下问题解决.
上传文件类型的限制:
$config['allowed_types'] = 'txt|php|cdr|gif|jpg|png';
OK现在这个是可以上传 txt|php|cdr文件的
起初我是这样设置的
$config['allowed_types'] = 'gif|jpg|png|txt|php|cdr';
把要限制上传的文件放到了 图片类型的后面
Codeigniter上传图片出现“You did not select afile to upload”的解决办法,需要的朋友可以参考下
Codeigniter自带的文件上传类非常好用,您可以设置指定上传某类型的文件及指定大小的文件。不过在使用的过程中需要注意一些小问题,否则会让你感到措手不及。
很多开发人员在使用Codeigniter的upload上传类时候,总是出现“You did not select a fileto upload”错误,从代码里面看并没有什么错误,这是怎么回事呢?
1、文件域的名字被修改
方法一:
例如将Views文件的文件域的名字修改成了fileimg,而CI默认的文件域的名字是userfile,即形如:。这个是不能修改的,否则就会找不到上传文件。
原理就是:$this->upload->do_upload()默认上传文件的表单名为userfile。
方法二:
其实还有解决方法,如果文件域是其他名字。比如:
将
改为
这样就可以了!
2、form表单类型
form表单需要使用form_open_multipart,形成HTML的时候会增加了一个属性enctype="multipart/form-data",这个其实是常识了,注意一下就好。