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

[已解决] 关于上传类型的判断

[复制链接]
发表于 2009-11-22 11:23:56 | 显示全部楼层 |阅读模式
我在做一个上传,一次最多同时上传2个文件,但是我想规定第一个input框是上传图片,第二个input是上传视频或音频,这个属于多文件上传,但是又是分开判断文件类型。

我的部分代码是这样的:

Controller

............................................................

         function do_uoload(){
                // upload image
                if(!empty($_FILES['photo']['name'])){
                    $photo = $this->_up_file('photo','img');
                    if(is_array($photo)){
                        $data['error'] = $photo;
                        $this->load->view('admin/main',$data);
                    }
                }
               
                // upload file
                if(!empty($_FILES['file']['name'])){
                    $file = $this->_up_file('file','av');
                    if(is_array($file)){
                        $data['error'] = $file;
                        $this->load->view('admin/main',$data);
                    }
                }
       }

     function _up_file($name,$type){
        $config['new_name']      = date('YmdHis');
        $config['upload_path']   = './uploads/';
        if($type == 'img'){
            $config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|bmp';
        }else{
            $config['allowed_types'] = 'mp3|rm|ram|wav|avi|mpeg|mpg|mov|qt';
        }
        
        $this->load->library('upload', $config);
        
        if ( ! $this->upload->do_upload($name)){
            return array('error' =>  $this->upload->display_errors() );
        }else{
            $success = $this->upload->data();
            return $success['file_name'];
        }
    }


............................................................



Views

............................................................
                <tr>
                    <td><label for="photo">Photo</label></td>
                    <td><input type="file" id="photo" name="photo" value="" /></td>
                </tr>
                <tr>
                    <td><label for="file">Audio Or Video</label></td>
                    <td><input type="file" id="file" name="file" value="" /></td>
                </tr>   

<?php if(!empty($error['error'])){ echo $error['error']; }else{ echo "Successful !"; } ?>

............................................................

另外,我用的是改良版的upload文件(允许自定义名)


我的测试结果如下:

第一次:  我只上传   id="photo" 框 ,  id="file" 框为空,  结果:非图片不允许上传  ------ 达到我想要的效果。
第二次:  我只上传   id="file" 框 ,  id="photo" 框为空,  结果:非视频或音频不允许上传  ------ 达到我想要的效果。
第三次:  我一起上传,两个框均为图片,  结果:上传成功 ,只判断第一个框的文件类型,不判断第二个文件类型------ 没有达到我想要的效果。


这个就是我的问题,第一个框,好像干扰了下面的判断。  不知道我错在哪里了? 谁可以指点一下?
 楼主| 发表于 2009-11-22 11:27:52 | 显示全部楼层
感觉上,好像要清除一下结果集,就好像php5里面的 __destruct();  但是不知道在codeigniter里面怎么做?
发表于 2009-11-22 12:04:12 | 显示全部楼层
请仔细看手册!
$this->load->library('upload', $config);
这句话只会执行一次,也就是只会初始化一次 config 参数。(CI 的 load 都有规避机制,只会装载一次实例)
第二次设置 config 参数要用
$this->upload->initialize($config);

还是希望楼主把手册看仔细,并有时间多看一下 CI 源码。
 楼主| 发表于 2009-11-22 12:33:01 | 显示全部楼层
谢谢,我最近一直都要学习,现在是边做边学,用到什么学什么,否则只是看,没有什么目标,太空洞了。 谢谢您的建议,实在羞愧啊~
发表于 2012-7-4 11:34:06 | 显示全部楼层
Hex 发表于 2009-11-22 12:04
请仔细看手册!
$this->load->library('upload', $config);
这句话只会执行一次,也就是只会初始化一次 con ...

谢谢HEX.

本版积分规则