0

在我的 Codeigniter 控制器中,有以下验证文件上传的私有函数。

     private function avatar_file_validation()
     {
        $config['upload_path'] = './uploads/avatars/';
        $config['allowed_types'] = 'jpg|png';
        $config['overwrite'] = TRUE; //overwrite user avatar
        $config['max_size'] = '800'; //in KB

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

        if (! $this->upload->do_upload('avatar_upload'))
        {
            $error_data = array('error' => $this->upload->display_errors());

            $this->avatar_view($error_data); //loads view

            return FALSE;
        }

     }

如果上传时发生错误,我想停止此功能继续

function upload_avatar()
{

    //some code

    if($_FILES['entry_upload']['error'] !== 4) //if file added to file field
    {
        $this->avatar_file_validation(); //if returns FALSE stop code
    }

    //code continues: adds data to database, redirects

}

但是,即使返回 false,该函数也会继续。它仅在我在 1 个函数中使用整个代码时才有效,但我需要将它们分开,因为我将在多个函数中使用上传验证。我在这里做错了什么?

4

2 回答 2

2

该表达式return FALSE;仅适用于函数avatar_file_validation()。如果你想在upload_avatar()上传失败时停止代码,你应该检查输出,avatar_file_validation()如果它等于FALSE,也从那个函数返回。

例如:

function upload_avatar()
{
    //some code

    if($_FILES['entry_upload']['error'] !== 4) //if file added to file field
    {
        if(!$this->avatar_file_validation()) //if returns FALSE stop code
            return FALSE;
    }

    //code continues: adds data to database, redirects
}
于 2012-07-09T19:15:58.203 回答
2
function upload_avatar()
{

    //some code

    if(!$_FILES['entry_upload']['error'] !== 4) //if file added to file field
    {
        if($this->avatar_file_validation()){
             return FALSE;
         }
    }

    //code continues: adds data to database, redirects

}
于 2012-07-09T19:16:30.487 回答