public static function copy($from, $to, $overwrite = true)
    {
        if (!file_exists($from)) {
            throw PathException::createUsingFormat(
                'The path "%s" does not exist.',
                $from
            );
        }

        $count = 0;

        if (is_file($from)) {
            if (file_exists($to) && !$overwrite) {
                return $count;
            }

            if (!@copy($from, $to)) {
                throw PathException::createUsingLastError();
            }

            $count++;
        } else {
            if (!file_exists($to) && !@mkdir($to)) {
                throw PathException::createUsingLastError();
            }

            if (!($handle = @opendir($from))) {
                throw PathException::createUsingLastError();
            }

            $empty = true;

            while (false !== ($entry = readdir($handle))) {
                if (('.' === $entry) || ('..' === $entry)) {
                    continue;
                }

                $empty = false;

                $count += self::copy(
                    $from . DIRECTORY_SEPARATOR . $entry,
                    $to . DIRECTORY_SEPARATOR . $entry,
                    $overwrite
                );
            }

            if ($empty) {
                $count++;
            }

            closedir($handle);
        }

        return $count;
    }
public static function copy($from, $to, $overwrite = true)
    {
        if (!file_exists($from)) {
            throw PathException::createUsingFormat(
                'The path "%s" does not exist.',
                $from
            );
        }

        $count = 0;

        if (is_file($from)) {
            if (file_exists($to) && !$overwrite) {
                return $count;
            }

            if (!@copy($from, $to)) {
                throw PathException::createUsingLastError();
            }

            $count++;
        } else {
            if (!file_exists($to) && !@mkdir($to)) {
                throw PathException::createUsingLastError();
            }

            if (!($handle = @opendir($from))) {
                throw PathException::createUsingLastError();
            }

            $empty = true;

            while (false !== ($entry = readdir($handle))) {
                if (('.' === $entry) || ('..' === $entry)) {
                    continue;
                }

                $empty = false;

                $count += self::copy(
                    $from . DIRECTORY_SEPARATOR . $entry,
                    $to . DIRECTORY_SEPARATOR . $entry,
                    $overwrite
                );
            }

            if ($empty) {
                $count++;
            }

            closedir($handle);
        }

        return $count;
    }
public static function copy ( $ from , $ to , $ overwrite = true ) { if ( ! file_exists ( $ from ) ) { throw PathException :: createUsingFormat ( 'The path "%s" does not exist.' , $ from ) ; } $ count = 0 ; if ( is_file ( $ from ) ) { if ( file_exists ( $ to ) && ! $ overwrite ) { return $ count ; } if ( ! @ copy ( $ from , $ to ) ) { throw PathException :: createUsingLastError ( ) ; } $ count ++ ; } else { if ( ! file_exists ( $ to ) && ! @ mkdir ( $ to ) ) { throw PathException :: createUsingLastError ( ) ; } if ( ! ( $ handle = @ opendir ( $ from ) ) ) { throw PathException :: createUsingLastError ( ) ; } $ empty = true ; while ( false !== ( $ entry = readdir ( $ handle ) ) ) { if ( ( '.' === $ entry ) || ( '..' === $ entry ) ) { continue ; } $ empty = false ; $ count += self :: copy ( $ from . DIRECTORY_SEPARATOR . $ entry , $ to . DIRECTORY_SEPARATOR . $ entry , $ overwrite ) ; } if ( $ empty ) { $ count ++ ; } closedir ( $ handle ) ; } return $ count ; }
Copies an existing file or directory path to a destination.

@param string  $from      The path to copy from.
@param string  $to        The path to copy to.
@param boolean $overwrite Overwrite existing files?

@return integer The number of files or empty directories copied.

@throws Exception
@throws PathException If the path could not be copied.
Copies an existing file or directory path to a destination .