找了半天解决方案,网上基本都是互相抄,改来改去也没效果,因此自己总结一下。
PHP版本:PHP/7.3.4
Laravel版本:V6+
Laravel-admin版本:1.8.17
- 修改
config/admin.php
/*
|--------------------------------------------------------------------------
| Laravel-admin upload setting
|--------------------------------------------------------------------------
|
| File system configuration for form upload files and images, including
| disk and upload path.
|
*/
'upload' => [
// Disk in `config/filesystem.php`.
'disk' => 'public',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
],
2.修改config/filesystems.php
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
],
'admin' => [
'driver' => 'local',
// 'root' => public_path('upload'),
'root' => public_path('app/public'),
'visibility' => 'public',
'url' => env('APP_URL').'/upload',
],
],
3.在需要上传图片的控制器中调用上传图片的方法
/**
* 上传图片专用
* Author:williamslife@icloud.com
*/
public function upload_img(Request $request, $fileInputName="image",$path="")
{
if ($request->isMethod('POST') || $request->isMethod('PUT')) { //判断文件是否是 POST的方式上传
$tmp = $request->file($fileInputName);
// $path = '/article'; //public下的article
$FileType = $tmp->getClientOriginalExtension(); //获取文件后缀
$FilePath = $tmp->getRealPath(); //获取文件临时存放位置
$FileName = date('Y-m-d'). "/" . uniqid() . '.' . $FileType; //定义文件名
Storage::disk('public')->put($FileName, file_get_contents($FilePath)); //存储文件
return $path . '/' . $FileName; //文件路径
}
}
4.删除storage
在public下的软连接(如果有,执行此操作)
5.清除配置缓存
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
6.重新配置链接
$ php artisan storage:link
The [public/storage] directory has been linked.