Pinkie
Pinkie

Reputation: 10256

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

Upvotes: 0

Views: 1076

Answers (2)

vascowhite
vascowhite

Reputation: 18430

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.

Upvotes: 2

Tonto
Tonto

Reputation: 322

Remember when you call objects own function inside the object, use the $this pointer variable.

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

Change to

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

Would be also good if you told us which line the error is happening at. Would be also nice to see the full error log.

Upvotes: 1

Related Questions