用户
 找回密码
 入住 CI 中国社区
搜索
查看: 23970|回复: 12
收起左侧

关于CI文件上传的几个问题.

[复制链接]
发表于 2009-5-22 17:49:37 | 显示全部楼层 |阅读模式
今天花了不少时间弄CI文件上传,把我遇到的几个问题及解决方法和大家分享下:
路径问题:刚开始我的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';
把要限制上传的文件放到了 图片类型的后面
测试报错
error:The filetype you are attempting to upload is not allowed.
呵呵,是不是觉得很怪.
参考地址:http://codeigniter.com/forums/viewthread/75310/ 里面有原因解释不过我英语不怎么好,看的懂大致的意思.希望翻译能力强的人翻译下比如(Hex)
发表于 2015-10-8 16:32:50 | 显示全部楼层
The filetype you are attempting to upload is not allowed.     上传的文件类型未被允许 $config['allowed_types'] = 'txt|php|cdr|gif|jpg|png';   这里改下。参考手册
发表于 2016-4-8 08:57:06 | 显示全部楼层
把MP3放到前面怎么还是不行啊
发表于 2014-8-18 10:25:40 | 显示全部楼层
楼主好强大,新手来学习的
发表于 2009-5-29 00:59:38 | 显示全部楼层

嗯.最近我也要用到了,来看看
发表于 2009-9-25 13:42:46 | 显示全部楼层
感谢兄台,已经解决自己的问题,getcwd()很好用
发表于 2009-12-14 16:45:46 | 显示全部楼层
其实是可以不用getcwd()函数的,因为CI是统一入口的,所以index.php中的getcwd() . 'upload/'其实就是根目录下的'./upload/',当然前提是index.php与upload文件夹在同一目录下。
但愿我上面说的是对的,呵呵。
发表于 2010-3-15 12:25:27 | 显示全部楼层
还是没解决……囧,路径还是没找到!
发表于 2010-8-7 00:16:22 | 显示全部楼层
回复 6# ivanlw


    现在解决了吗?
发表于 2010-8-7 00:16:58 | 显示全部楼层
回复 4# test


    怎么解决的啊??
发表于 2010-11-22 12:11:32 | 显示全部楼层
$config['upload_path'] ='./upload/',这样写是没有问题的,前提是upload和主控制器index.php在同一文件夹下面。我按照手册的案例写的代码,可是会出现过The upload path does not appear to be valid.的错误提示,按照楼主的方法还是不能解决,后来我发现原来不是路径的问题,也不是手册和楼主说的不对,是php配置环境的问题,都是相同的上传文件代码在集成的php开发环境(vista操作系统,appServ)下就会报错,但是我换了另外一台电脑(winxp操作系统,php,apache,mysql是手动配置的)调试就没有问题了
发表于 2012-7-30 21:16:26 | 显示全部楼层
In my config array, I had it like this:

..
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
$this->load->library('upload', $config);
What you want to do is, put the mp3 in the front, before all the other file types, like so:

..
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|jpe|png';
$this->load->library('upload', $config);
Heres Why:
Here is the part of the function in the Upload.php Core Library that checks for allowed filtypes. I only pasted the important part (the first part)

function is_allowed_filetype()
{
   
    $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
    foreach ($this->allowed_types as $val)
    {
        $mime = $this->mimes_types(strtolower($val));

        // Images get some additional checks
        if (in_array($val, $image_types))
        {
            if (getimagesize($this->file_temp) === FALSE)
            {
                    return FALSE;
            }
        }
...
Heres what happens. If you have your allowed type with the image file extension first
like

$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
The for loop will check them in order. It will start with gif, then jpg, then jpeg, etc.

The first IF statement in the loop, checks to see if the allowed filetype is an image filetype (as there is a variable array containing image types above it $image_types)

well gif (the first in my allowed types) matches, so into the if statement it goes.

The next if statement, it checks for a valid image, by calling the function getimagesize(). Well, I uploaded an MP3, which is not an image, so getimagesize returns false… into the if statement.

return false. returning false ends the forloop right on the spot returning saying its an invalid filetype, and so it doesn’t continue on the rest of your allowed mime types to see of your MP3 is allowed or not.

if you upload an mp3, and an image format is listed first an your allowed_types config array, it will return Filetype not allowed as it will fail the test of the image types.


Now, if mp3 is listed first, and you upload an image, your ok, because the the first If statement will be skipped while checking the mp3 filetype, as the loop comes around again, it will check the image, getimagesize() wont return false, etc.

So it seems the best solution, is to always list image filetype LAST in your allowed_types config array.

Hope this helps.
发表于 2012-7-30 21:37:09 | 显示全部楼层
77036553 发表于 2012-7-30 21:16
In my config array, I had it like this:

..

试着翻一下:
“你需要把mp3文件类型放在前面,像这样:
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|jpe|png';
$this->load->library('upload', $config);

原因如下:
下面这个是核心库文件中检查可上传文件类型的函数upload.php,我只黏贴重要的部分(第一部分):
function is_allowed_filetype()
{
   
    $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
    foreach ($this->allowed_types as $val)
    {
        $mime = $this->mimes_types(strtolower($val));

        // Images get some additional checks
        if (in_array($val, $image_types))
        {
            if (getimagesize($this->file_temp) === FALSE)
            {
                    return FALSE;
            }
        }
...
我们来看看这个函数是怎么执行的。如果你的上传文件类型是这样写的:
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
foreach循环会按顺序检查它们,由gif开始,然后是jpg,然后jpeg,以此类推,最后是mp3,
循环中的第一个if语句(statement)检查允许上传的文件类型是不是图片文件,
第一个是gif类型(你设定的第一个允许上传的文件类型)符合,所以进入if语句里继续执行,
第二个if语句检查它是不是一个有效的图片,通过调用函数getimagesize(),
好,如果我上传一个MP3,不是一个有效的图片,那么getimagesize()函数返回false,
再回到第一个if语句,再返回false,
最后结束foreach循环,报错说无效的文件类型(an invalid filetype),后面的那些检查你的mp3文件是否符合上传文件类型的语句就不再执行了,
现在如果你把mp3写在第一,像这样:
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|jpe|png';
你上传图片,没有问题,在检查mp3文件的时候,会跳过第一个if语句,直到再次执行循环的时候才会去检查图片文件,getimagesize()函数不会返回false”

-------------------------------

本版积分规则