3

I have a recursive copy function that should copy all files from a directory to another, but it gives an error. This is the function;

public function cpy($source, $dest){
    if(is_dir($source)) {
        $dir_handle=opendir($source);
        while($file=readdir($dir_handle)){
            if($file!="." && $file!=".."){
                if(is_dir($source."/".$file)){
                    if(!is_dir($dest."/".$file)){
                        mkdir($dest."/".$file);
                    }
                    cpy($source."/".$file, $dest."/".$file);
                } else {
                    copy($source."/".$file, $dest."/".$file);
                }
            }
        }
        closedir($dir_handle);
    } else {
        copy($source, $dest);
    }
}

The error given is Fatal error: Call to undefined function cpy() in /var/www/Deployer/include/deployer.class.php on line 14

This function is also in a class.

Core
  • 335
  • 1
  • 11
  • 1
    where is `cpy` is defined and what is in deployer.class.php line 14? – Iłya Bursov Dec 30 '14 at 17:20
  • At least try to understand the error message. Look you made typo `cpy($source."/".$file, $dest."/".$file);` – Shaiful Islam Dec 30 '14 at 17:20
  • @Lashane `cpy` is defined directly at the top of the code shown in the question... also, line 14 is the line calling `cpy` – Core Dec 30 '14 at 17:21
  • @Core it looks like it class method, so you should use `$this->cpy(...` to call it – Iłya Bursov Dec 30 '14 at 17:23
  • @ShaifulIslam this is **recursive copy** the `copy` function would not work. – Core Dec 30 '14 at 17:23
  • it should not be `cpy($source."/".$file, $dest."/".$file);` because you are calling a function of a class. See Riad's solution.Hope you will learn first how to call member function of a class from another member function. – Shaiful Islam Dec 30 '14 at 17:28

3 Answers3

2

Are you using any class? then you should use:

$this->cpy(...)

Or remove the public infront of the function.

Riad
  • 3,822
  • 5
  • 28
  • 39
2

Use $this ->

public function cpy($source, $dest){
    if(is_dir($source)) {
        $dir_handle=opendir($source);
        while($file=readdir($dir_handle)){
            if($file!="." && $file!=".."){
                if(is_dir($source."/".$file)){
                    if(!is_dir($dest."/".$file)){
                        mkdir($dest."/".$file);
                    }
                    $this->cpy($source."/".$file, $dest."/".$file);// use the $this keyword here
                } else {
                    copy($source."/".$file, $dest."/".$file);
                }
            }
        }
        closedir($dir_handle);
    } else {
        copy($source, $dest);
    }
}
Riad
  • 3,822
  • 5
  • 28
  • 39
Surya
  • 454
  • 6
  • 15
0

This is probably beacause you are in a class.

Try this instead :

self::cpy($source."/".$file, $dest."/".$file);
Brice Favre
  • 1,511
  • 1
  • 15
  • 34
  • Yeah but i think this is more apporiate and PHP allow to use it even if we don't add the static keywords http://stackoverflow.com/questions/151969/when-to-use-self-vs-this – Brice Favre Dec 30 '14 at 17:24