| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\librarys\storage\drivers;
- use app\librarys\storage\exceptions\StorageException;
- use app\librarys\storage\helpers\UrlConverter;
- class Local extends BaseDriver
- {
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->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);
- }
- }
|