duanji1026 2012-07-09 19:02
浏览 35
已采纳

php从另一个函数中的私有函数返回FALSE

In my Codeigniter controller have the following private function which validates file uploads.

     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;
        }

     }

if an error occurs in uploading I want to stop this function from continuing

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

}

However the function continues even when returning false. It works only when I use the entire code in 1 function but I need to separate them as I'll be using the upload validation in multiple functions. What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • drrhr20884 2012-07-09 19:15
    关注

    The expression return FALSE; only applies to the function avatar_file_validation(). If you want to stop the code in upload_avatar() when the upload fails, you should check the output of avatar_file_validation() and if it equals FALSE, return from that function as well.

    For example:

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址