【问题标题】:Codeigniter : image upload validationCodeigniter:图片上传验证
【发布时间】:2016-04-06 01:20:44
【问题描述】:

我想验证上传的图像,所以当sizetype 不像config 时,我想显示一些警报或者可能是错误消息,我已经尝试了类似下面的代码但如果sizetype 不允许而不是显示错误消息或警报,它只是使按钮无法点击,我做错了什么?,我一直在互联网上搜索它,但我仍然不明白它的原因我最近才学习CIPHP 更不用说英语不是我的第一个,我相信它可以使用javascriptcontroller 本身来完成

控制器

public function Upload($id){
    $upload = $this->input->post('fotoDsn');
    //Foto Set
    $photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
    $config['upload_path'] = './assets/img/dosen/';
    $config['allowed_types'] = 'gif||jpg||png';
    $config['max_size'] = '1000';
    $config['file_name'] = $photoName;
    $this->load->library('upload',$config);
    if($this->upload->do_upload('userfile')){           
        $upload = 1;
    }else if (!$this->upload->do_upload('userfile')){
        $upload = 2;
    }
    if($upload==1){
        $data   = array(
                'foto_dosen'=>$photoName);
        $insert = $this->MDosen->update(array('id'=>$id),  $data);
        if($insert){
            echo 1;
        }else{
            echo 2;
        }
    }else if($upload==2){//if upload fail
        alert('error');
        echo "failed";
        $errors = $this->upload->display_errors('<p>','failed try again','</p');
        flashMsg($errors);
    }
}

查看

<!-- Upload Modal Structure Start -->
<div id="modalUpload" class="modal" style="width: 40%; height: auto">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
             <i class="medium material-icons prefix">assignment_ind</i>
                <h4 class="modal-title"> Upload Foto</h4>
            </div>
            <div class="modal-body">
                <form action="upload" id="tambahFormUpload" method="post" enctype="multipart/form-data">
                    <div class="file-field input-field">
                        <div class="btn">
                            <span>File</span>
                            <input type="file" name="userfile">
                        </div>
                        <div class="file-path-wrapper">
                            <input class="file-path validate" type="text">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" >Simpan</button>
                    </div>  
                </form>
            </div>
        </div>  
    </div>
</div>
<!-- Upload Modal Structure End -->

【问题讨论】:

    标签: jquery html css codeigniter


    【解决方案1】:

    您的代码没有问题。它在codeigniter-3.0.6 中完美运行。看了错误日志,觉得你用的codeigniter版本有bug。

    参考这里:https://github.com/bcit-ci/CodeIgniter/issues/4137

    修复在这里:https://github.com/bcit-ci/CodeIgniter/commit/84f24c23baf5ea45c30c4ab3cbc57cd846ea0f56

    我认为您必须更改位于C:\xampp\htdocs\PETTA\system\core\Exceptions.php 中的Exceptions.php 文件的行号190

    来自

    public function show_exception(Exception $exception)
    

    收件人

    public function show_exception($exception)
    

    更新

    我再次测试了您的代码。这是您的代码的工作副本。

        function upload()
        {
            echo'<div id="modalUpload" class="modal" style="width: 40%; height: auto">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                         <i class="medium material-icons prefix">assignment_ind</i>
                            <h4 class="modal-title"> Upload Foto</h4>
                        </div>
                        <div class="modal-body">
                            <form action="'.base_url('costing/upload').'" id="tambahFormUpload" method="post" enctype="multipart/form-data">
                                <div class="file-field input-field">
                                    <div class="btn">
                                        <span>File</span>
                                        <input type="file" name="userfile">
                                    </div>
                                    <div class="file-path-wrapper">
                                        <input class="file-path validate" type="text">
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-primary" >Simpan</button>
                                </div>  
                            </form>
                        </div>
                    </div>  
                </div>
            </div>';
    
    
    
            // $upload = $this->input->post('fotoDsn');
                //Foto Set
            if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
                $photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
                $config['upload_path'] = './assets/img/dosen/';
                $config['allowed_types'] = 'gif||jpg||png';
                $config['max_size'] = '1000';
                $config['file_name'] = $photoName;
                $this->load->library('upload',$config);
                if($this->upload->do_upload('userfile')){           
                    $upload = 1;
                }else if (!$this->upload->do_upload('userfile')){
                    $upload = 2;
                }
                if($upload==1){
                    // $data   = array(
                    //         'foto_dosen'=>$photoName);
                    // $insert = $this->MDosen->update(array('id'=>$id),  $data);
                    // if($insert){
                    //     echo 1;
                    // }else{
                    //     echo 2;
                    // }
                }else if($upload==2){//if upload fail
                        
                    // echo "failed";
                     $errors = $this->upload->display_errors();
                     echo '<script type="text/javascript">
                                alert("error, failed try again.'.$errors.'");
                                </script>';
                    // flashMsg($errors);
                }
              }
    
        }
    

    因为忙于做另一项工作,我没有在这里使用任何视图文件,只是控制器中的一个功能。您必须将form action="'.base_url('costing/upload').'" 更改为您喜欢的控制器/功能,没有名为fotoDsn 的输入,所以我评论了这一行:$upload = $this-&gt;input-&gt;post('fotoDsn');,还注释掉了数据库更新部分,因为我没有模型或表,还更新了错误报告功能,因为我不知道flashMsg($errors) 功能的作用。您必须根据自己的需要修改这些更改。

    【讨论】:

    • 我已经编辑了,如果你能发布错误,这将非常有助于解决问题。谢谢
    • Fatal error: Uncaught TypeError: Argument 1 passed to CI_Exceptions::show_exception() must be an instance of Exception, instance of Error given, called in C:\xampp\htdocs\PETTA\system\core\Common.php on line 658 and defined in C:\xampp\htdocs\PETTA\system\core\Exceptions.php:190 Stack trace: #0 C:\xampp\htdocs\PETTA\system\core\Common.php(658): CI_Exceptions-&gt;show_exception(Object(Error)) #1 [internal function]: _exception_handler(Object(Error)) #2 {main} thrown in C:\xampp\htdocs\PETTA\system\core\Exceptions.php on line 190 ,还是一样的错误
    • 我又改了,误会见谅。如果还有其他错误,请在此处发布。
    • 现在没有错误,但是按钮变得不可点击,我希望当规则错误时它会显示类似于警报的弹出消息,我该怎么做?由于时间不多,我稍后再研究,但这个问题真的很困扰我
    • 您必须发布其中包含按钮的表单的代码。
    猜你喜欢
    • 2015-11-16
    • 2017-05-28
    • 2017-07-25
    • 1970-01-01
    • 2013-08-23
    • 2011-06-08
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多