我在使用这个基于 CI 文档中的示例的简单 Codeigniter 图像上传时遇到了问题。

出了什么问题:

1.) 如果上传的文件类型不正确,它不会返回错误。它将我所有的 post 变量设置为 null 并继续处理。

2.) 我能够上传 .sql 和一些(但不是全部).exe 文件,即使允许的文件类型仅限于 jpg、gif 和 png。

因此,拒绝允许的文件类型并不是常见的问题,而是过于宽容,并且没有在应该返回错误时返回。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    $photo = $this -> input -> post ( “photo” ) ;

    $config [ ‘upload_path’ ] = ‘./profile_img/’ ;
    $config [ ‘allowed_types’ ] = ‘gif|jpg|png’ ;
    $config [ ‘max_size’ ] = ‘100’ ;
    $config [ ‘max_width’ ]   = ‘1024’ ;
    $config [ ‘max_height’ ]   = ‘768’ ;

    $this -> load -> library ( ‘upload’ , $config ) ;

    if ( isset ( $_FILES [ ‘userfile’ ] ) && $_FILES [ ‘userfile’ ] [ ‘size’ ] > 0 ) {  
        if ( ! $this -> upload -> do_upload ( ‘userfile’ ) )
        {
                $error = $this -> upload -> display_errors ( ) ;
                $this -> session -> set_userdata ( “message” , $error ) ;
                header ( “Location: /page/account” ) ;
        }
        else
        {
                $upload_data = $this -> upload -> data ( ) ;
                $photo = $upload_data [ ‘file_name’ ] ;
        }
    }

    $data = array (
        ‘first_name’ => $this -> input -> post ( “first_name” ) ,
        ‘last_name’ => $this -> input -> post ( “last_name” ) ,
        ‘username’ => $this -> input -> post ( “username” ) ,
        ‘default_meeting_duration’ => $this -> input -> post ( “default_meeting_duration” ) ,
        ‘notification_type’ => $this -> input -> post ( “notification_type” ) ,
        ‘photo’ => $photo
    ) ;

    $this -> user_model -> update ( $data , $this -> ion_auth -> get_user_id ( ) ) ;
    $this -> session -> set_userdata ( “message” , “Account Settings Saved” ) ;
    header ( “Location: /page/account” ) ;



相关讨论

  • 首先删除 if 条件然后尝试


奇怪的是,解决方案是在错误条件下的第一次重定向之后插入 exit()。

显然,这种行为是这样的,即使发现错误条件,重定向也会被忽略,并且脚本的其余部分会被执行。显然是 CI 中的一个令人费解且难以找到的错误。


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。