yii\helpers\BaseFileHelper::copyDirectory PHP Method

copyDirectory() public static method

The files and sub-directories will also be copied over.
public static copyDirectory ( string $src, string $dst, array $options = [] )
$src string the source directory
$dst string the destination directory
$options array options for directory copy. Valid options are: - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775. - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting. - filter: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. The callback can return one of the following values: * true: the directory or file will be copied (the "only" and "except" options will be ignored) * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored) * null: the "only" and "except" options will determine whether the directory or file should be copied - only: array, list of patterns that the file paths should match if they want to be copied. A path matches a pattern if it contains the pattern string at its end. For example, '.php' matches all file paths ending with '.php'. Note, the '/' characters in a pattern matches both '/' and '\' in the paths. If a file path matches a pattern in both "only" and "except", it will NOT be copied. - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. A path matches a pattern if it contains the pattern string at its end. Patterns ending with '/' apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches both '/' and '\' in the paths. - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true. - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. If the callback returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file to be copied from, while `$to` is the copy target. - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file copied from, while `$to` is the copy target.
    public static function copyDirectory($src, $dst, $options = [])
    {
        $src = static::normalizePath($src);
        $dst = static::normalizePath($dst);
        if ($src === $dst || strpos($dst, $src . DIRECTORY_SEPARATOR) === 0) {
            throw new InvalidParamException('Trying to copy a directory to itself or a subdirectory.');
        }
        if (!is_dir($dst)) {
            static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
        }
        $handle = opendir($src);
        if ($handle === false) {
            throw new InvalidParamException("Unable to open directory: {$src}");
        }
        if (!isset($options['basePath'])) {
            // this should be done only once
            $options['basePath'] = realpath($src);
            $options = self::normalizeOptions($options);
        }
        while (($file = readdir($handle)) !== false) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            $from = $src . DIRECTORY_SEPARATOR . $file;
            $to = $dst . DIRECTORY_SEPARATOR . $file;
            if (static::filterPath($from, $options)) {
                if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) {
                    continue;
                }
                if (is_file($from)) {
                    copy($from, $to);
                    if (isset($options['fileMode'])) {
                        @chmod($to, $options['fileMode']);
                    }
                } else {
                    // recursive copy, defaults to true
                    if (!isset($options['recursive']) || $options['recursive']) {
                        static::copyDirectory($from, $to, $options);
                    }
                }
                if (isset($options['afterCopy'])) {
                    call_user_func($options['afterCopy'], $from, $to);
                }
            }
        }
        closedir($handle);
    }

Usage Example

 /** @inheritdoc */
 public function bootstrap($app)
 {
     /* Config Translation */
     if (!isset($app->get('i18n')->translations['adminlte*'])) {
         $app->get('i18n')->translations['adminlte*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages'];
     }
     /* Config Theme */
     if (!isset($app->view->theme)) {
         $app->view->theme = new \yii\base\Theme(['pathMap' => ['@app/views/layouts' => '@cjtterabytesoft/adminlte/basic/views/layouts', '@app/views/site' => '@cjtterabytesoft/adminlte/basic/views/site']]);
     }
     /* Copy Avatar Images */
     if (\yii\helpers\BaseFileHelper::filterPath(\Yii::getAlias('@app/web/images'), $options = [])) {
         \yii\helpers\BaseFileHelper::copyDirectory(\Yii::getAlias('@cjtterabytesoft/adminlte/basic/images/'), \Yii::getAlias('@app/web/images'));
     }
     /* Config Params */
     if (!isset($app->params['adminEmail'])) {
         $app->params['adminEmail'] = '*****@*****.**';
     }
     if (!isset($app->params['AdminLTESkin'])) {
         $app->params['AdminLTESkin'] = 'skin-yellow';
     }
     if (!isset($app->params['Author'])) {
         $app->params['Author'] = '2015 - Wilmer Arambula';
     }
     if (!isset($app->params['Facebook_Account'])) {
         $app->params['Facebook_Account'] = 'https://www.facebook.com/username';
     }
     if (!isset($app->params['Google_Account'])) {
         $app->params['Google_Account'] = 'https://www.google.com/+username';
     }
     if (!isset($app->params['Linkedin_Account'])) {
         $app->params['Linkedin_Account'] = 'https://www.linkedin.com/in/username';
     }
     if (!isset($app->params['Twitter_Account'])) {
         $app->params['Twitter_Account'] = 'https://twitter.com/username';
     }
     if (!isset($app->params['WebName'])) {
         $app->params['WebName'] = 'My Application';
     }
     if (!YII_ENV_TEST) {
         if (!isset($app->params['imagesurl_30'])) {
             $app->params['imagesurl_30'] = 'http://www.basic.tk/images/avatar/profile/30/icon-avatar.png';
         }
         if (!isset($app->params['imagesurl_60'])) {
             $app->params['imagesurl_60'] = 'http://www.basic.tk/images/avatar/profile/60/icon-avatar.png';
         }
     } else {
         if (!isset($app->params['imagesurl_30'])) {
             $app->params['imagesurl_30'] = 'http://localhost.basic/images/avatar/profile/30/icon-avatar.png';
         }
         if (!isset($app->params['imagesurl_60'])) {
             $app->params['imagesurl_60'] = 'http://localhost.basic/images/avatar/profile/60/icon-avatar.png';
         }
     }
 }
All Usage Examples Of yii\helpers\BaseFileHelper::copyDirectory