米
首页
心情
图集
所有
搜索
登录
推荐一款PHP支持文件及文件夹增删改查的composer包,删除目录,删除文件等
2021-04-02
57.01w热度
文件及文件夹的操作是必不可少的,删除文件,删除文件夹,追加文件等,虽然PHP有自带函数可以实现大部分功能,这里还是要给大家推荐一款支持目录操作的composer包。 ``` composer require league/flysystem ``` 使用说明: ``` 写文件 try { $filesystem->write($path, $contents, $config); } catch (FilesystemError | UnableToWriteFile $exception) { // handle the error } 写文件流 try { $filesystem->writeStream($path, $stream, $config); } catch (FilesystemError | UnableToWriteFile $exception) { // handle the error } 读取 try { $response = $filesystem->read($path); } catch (FilesystemError | UnableToReadFile $exception) { // handle the error } 删除 try { $filesystem->delete($path); } catch (FilesystemError | UnableToDeleteFile $exception) { // handle the error } 删除目录 try { $filesystem->deleteDirectory($path); } catch (FilesystemError | UnableToDeleteDirectory $exception) { // handle the error } 读取目录下文件 try { $listing = $filesystem->listContents($path, $recursive); /** @var \League\Flysystem\StorageAttributes $item */ foreach ($listing as $item) { $path = $item->path(); if ($item instanceof \League\Flysystem\FileAttributes) { // handle the file } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) { // handle the directory } } } catch (FilesystemError $exception) { // handle the error } 文件是否存在 try { $fileExists = $filesystem->fileExists($path); } catch (FilesystemError | UnableToRetrieveMetadata $exception) { // handle the error } 文件最后被访问时间 try { $lastModified = $filesystem->lastModified($path); } catch (FilesystemError | UnableToRetrieveMetadata $exception) { // handle the error } 文件或者文件夹大小 try { $fileSize = $filesystem->fileSize($path); } catch (FilesystemError | UnableToRetrieveMetadata $exception) { // handle the error } 创建目录 try { $filesystem->createDirectory($path, $config); } catch (FilesystemError | UnableToCreateDirectory $exception) { // handle the error } 剪切 try { $filesystem->move($source, $destination, $config); } catch (FilesystemError | UnableToMoveFile $exception) { // handle the error } 复制 try { $filesystem->copy($source, $destination, $config); } catch (FilesystemError | UnableToCopyFile $exception) { // handle the error } ``` 官方文档 https://flysystem.thephpleague.com/v2/docs/usage/filesystem-api/
注:原创不易,转载请注明出处(
http://www.micuu.com/new/1510.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-02-20
上一篇:
吉利汽车保养手册
下一篇:
ffmpeg拉流命令
▶
珍藏视频
▶
10分钟高效燃脂
▶
30天高效瘦脸操
▶
5分钟缓解颈椎操
友人
→
微博
→
全民K歌
→
唱吧
→
今日头条
→
悠悠网
→
科技小锅盖
→
彼岸桌面
→
阮一峰
→
laravel社区
→
V2ex
→
掘金
+
更多