setUrlCallback(new UrlConverter()); } public function put($localFile, $saveTo) { $saveTo = \Yii::$app->basePath . '/' . $saveTo; $saveDir = dirname($saveTo); try { if (!is_dir($saveDir) && !mkdir($saveDir, 0777, true)) { return false; } if (!copy($localFile, $saveTo)) { return false; } } catch (\Exception $ex) { throw new StorageException($ex->getMessage()); } $hostInfo = ''; if (\Yii::$app instanceof \yii\web\Application){ $hostInfo = \Yii::$app->request->hostInfo; } return $hostInfo . '/' . static::getRelativePath(\Yii::$app->basePath, $saveTo); } public static function getRelativePath($from, $to) { $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; $from = str_replace('\\', '/', $from); $to = str_replace('\\', '/', $to); $from = explode('/', $from); $to = explode('/', $to); $relPath = $to; foreach ($from as $depth => $dir) { if (strcasecmp($dir, $to[$depth]) === 0) { array_shift($relPath); } else { $remaining = count($from) - $depth; if ($remaining > 1) { $padLength = (count($relPath) + $remaining - 1) * -1; $relPath = array_pad($relPath, $padLength, '..'); break; } else { // $relPath[0] = './' . $relPath[0]; } } } return implode('/', $relPath); } }