laravel:文件操作(10.27.0)

一,相关文档:
https://learnku.com/docs/laravel/10.x/filesystem/14865
二,配置.env和config
1, .env中
IMG_HOST = "http://192.168.219.6"
IMG_DIR = "/var/www/html"
2,config/filesystems.php中:img部分是我们新添加配置
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'img'=>[
'driver' => 'local',
'root' => env('IMG_DIR')."/goodsImage",
'url' => env('IMG_HOST').'/goodsImage',
'visibility' => 'public',
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
三,php代码:
controller中:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
use Illuminate\Support\Facades\Storage;class NewsController extends Controller{ //操作文件 public function imgFile(){ // 取到磁盘实例 $disk = Storage::disk('img'); //判断文件是否存在 $filePath = "1641037085.jpg"; $exists = $disk->exists($filePath); echo "图片文件是否存在:".$exists."<br/>"; //得到图片url $url = $disk->url($filePath); echo "图片文件url:".$url."<br/>"; //复制文件 $filePathNew = "anew.jpg"; $disk->copy($filePath,$filePathNew); $existsNew = $disk->exists($filePathNew); echo "复制生成图片文件是否存在:".$existsNew."<br/>"; // 创建目录 $disk->makeDirectory('test'); //移动文件 第一个参数是要移动的文件,第二个参数是移动到哪里 $disk->move($filePathNew, 'test/'.$filePathNew); $existsMove = $disk->exists('test/'.$filePathNew); echo "移动的图片文件是否存在:".$existsMove."<br/>"; //删除文件 $disk->delete('test/'.$filePathNew); $existsMove2 = $disk->exists('test/'.$filePathNew); echo "删除后的图片文件是否存在:<br/>"; var_dump($existsMove2); } //下载文件 public function imgDown(){ // 取到磁盘实例 $disk = Storage::disk('img'); $filePath = "1641037085.jpg"; //下载 return $disk->download($filePath); } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/07/laravel-wen-jian-cao-zuo-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,测试效果:

五,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
浙公网安备 33010602011771号