| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace backend\modules\admin\controllers;
- use backend\controllers\AdminController;
- use common\models\common\Attachment;
- use \Yii;
- use yii\web\NotFoundHttpException;
- use yii\web\UploadedFile;
- class FileController extends AdminController
- {
- public $uploadFileName = 'file';
- protected $uploadDrive;
- /**
- *
- * @var \League\Flysystem\Filesystem
- */
- protected $filesystem;
- /**
- * 微信文件单独上传
- * @Author: lun
- * @DateTime: 2021/11/23 0023 15:12
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionUpload()
- {
- if (Yii::$app->request->isPost) {
- try {
- $file = UploadedFile::getInstanceByName($this->uploadFileName);
- if (!$file->getHasError()) {
- $stream = fopen($file->tempName, 'r+');
- $path = '/runtime/pem/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址
- $path = '/web/uploads/platform/' . md5(time()) .'.'. $file->getExtension();// 生成文件地址
- $config = ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')];// 重置配置
- $this->uploadDrive = Yii::$app->uploadDrive->local($config);
- $this->filesystem = $this->uploadDrive->entity();
- $result = $this->filesystem->writeStream($path, $stream);
- if (!$result) {
- throw new NotFoundHttpException('文件写入失败');
- }
- if (is_resource($stream)) {
- fclose($stream);
- }
- } else {
- throw new NotFoundHttpException('上传失败,可能文件太大了');
- }
- return $this->success('上传成功', ['path' => 'backend' . $path]);
- } catch (\Exception $e) {
- return $this->error('上传失败', $e->getMessage());
- }
- }
- }
- /**
- * 微信js文件上传
- * @Author: lun
- * @DateTime: 2022/7/26 0026 16:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionWechatJs()
- {
- if (Yii::$app->request->isPost) {
- try {
- $file = UploadedFile::getInstanceByName($this->uploadFileName);
- if (!$file->getHasError()) {
- $h5_path = Yii::$app->basePath.'/../h5';
- // 文件上传地址
- $path_config = [
- [
- 'path' => '/web/'.$file->name,// 重置配置
- 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@api')],// 重置配置
- ],
- [
- 'path' => '/web/'.$file->name,// 重置配置
- 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => Yii::getAlias('@backend')],// 重置配置
- ],
- [
- 'path' => $file->name,// 重置配置
- 'config' => ['drive' => Attachment::DRIVE_LOCAL, 'alias_path' => $h5_path],// 重置配置
- ],
- ];
- foreach ($path_config as $item) {
- $stream = fopen($file->tempName, 'r+');
- $item['config']['disable_asserts'] = true;
- $this->uploadDrive = Yii::$app->uploadDrive->local($item['config']);
- $this->filesystem = $this->uploadDrive->entity();
- $config = $this->filesystem->getConfig();
- $config->set('disable_asserts', true);// 设置覆盖上传
- $result = $this->filesystem->writeStream($item['path'], $stream);
- if (!$result) {
- throw new NotFoundHttpException('文件写入失败');
- }
- if (is_resource($stream)) {
- fclose($stream);
- }
- }
- } else {
- throw new NotFoundHttpException('上传失败,可能文件太大了');
- }
- $this->success('上传成功', ['path' => $file->name]);
- } catch (\Exception $e) {
- return $this->error('上传失败', $e->getMessage());
- }
- }
- }
- }
|