Search code examples
phpclassfunctionexpressionengine

php Class calling a function inside another function error


I have the following class with few functions inside. I'm getting an error when i call the get_dir_size() inside the Diskspace() function. It's not recognizing it. What am i doing wrong. This code is for creating an expressionengine plugin.

Fatal error: Call to undefined function get_dir_size()

class DiskSpace
{

    public $return_data = "";
    public function Diskspace()
    {
        $this->EE =& get_instance();

        $dir_name = $_SERVER['DOCUMENT_ROOT']."/userfiles/";
        /* 1048576 bytes == 1MB */
        $total_size= round((get_dir_size($dir_name) / 1048576),2) ;
         $this->return_data = $total_size;

    }

    public function get_dir_size($dir_name){
        $dir_size =0;
        if (is_dir($dir_name)) {
            if ($dh = opendir($dir_name)) {
                while (($file = readdir($dh)) !== false) {
                    if($file !="." && $file != ".."){
                        if(is_file($dir_name."/".$file)){
                            $dir_size += filesize($dir_name."/".$file);
                        }
                        /* check for any new directory inside this directory */
                        if(is_dir($dir_name."/".$file)){
                            $dir_size +=  get_dir_size($dir_name."/".$file);
                        }
                    }
                }
            }
        }
        closedir($dh);
        return $dir_size;
    }
}

Solution

  • Change the line that calls get_dir_size($dir_name) to :-

    $total_size= round(($this->get_dir_size($dir_name) / 1048576),2) ;
    

    There are a few more issues with this code What is this ? $this->EE = & get_instance(); and also you will get errors when you execute closedir($dh); if the if statement that it is declared in is not executed.