nzo / file-downloader-bundle
NzoFileDownloaderBundle 是一个用于从服务器和 Web 应用程序中安全且轻松地下载所有类型文件的 Symfony Bundle。您还可以在 Web 浏览器中读取/显示文件内容。
v4.2.0
2021-12-06 08:22 UTC
Requires
- php: >=7.1.3
- symfony/framework-bundle: ^4.4|^5.0|^6.0
- symfony/mime: ^4.4|^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.0
README
NzoFileDownloaderBundle 是一个用于从 服务器
和 Web 应用程序项目
安全且轻松地 下载
所有类型 文件
的 Symfony Bundle。您还可以在 Web 浏览器中 读取/显示
文件内容。
功能包括
- 本版本的包与 Symfony >= v4.4 兼容
- 在 Web 浏览器 中
读取/显示
文件内容。 - 从 Symfony
public
文件夹或自定义路径下载
所有类型的文件
。 - 下载时更改文件名称。
- 从 URL 下载文件
- 从 URL 获取文件扩展名
安装
通过 Composer
$ composer require nzo/file-downloader-bundle
在 config/bundles.php 中注册包(无 Flex)
// config/bundles.php return [ // ... Nzo\FileDownloaderBundle\NzoFileDownloaderBundle::class => ['all' => true], ];
使用方法
在 Web 浏览器中读取/显示文件内容
use Nzo\FileDownloaderBundle\FileDownloader\FileDownloader; class MyController extends AbstractController { private $fileDownloader; public function __construct(FileDownloader $fileDownloader) { $this->fileDownloader = $fileDownloader; // without autowiring use: $this->get('nzo_file_downloader') } // In this examples the "myfile.pdf" file exist in "public/myfolder/myfile.pdf". public function readFilesFromPublicFolder() { return $this->fileDownloader->readFile('myfolder/myfile.pdf'); } // Absolute PATH: public function readFilesFromAbsolutePath() { return $this->fileDownloader->readFileFromAbsolutePath('/home/user/myfile.pdf'); } }
下载文件
public function downloadFileFromPublicFolder() { return $this->fileDownloader->downloadFile('myfolder/myfile.pdf'); # change the name of the file when downloading: return $this->fileDownloader->downloadFile('myfolder/myfile.pdf', 'newName.pdf'); } // Absolute PATH: public function downloadFilesFromAbsolutePath() { return $this->fileDownloader->downloadFileFromAbsolutePath('/home/user/myfile.pdf'); # change the name of the file when downloading: return $this->fileDownloader->downloadFileFromAbsolutePath('/home/user/myfile.pdf', 'newName.pdf'); } }
从 URL 下载文件
public function downloadFileFromUrl(string $url, string $pathWhereToDownloadTheFile, ?string $customUserAgent = null) { $headers = ['Authorization: Basic auth']; $response = $this->fileDownloader->downloadFileFromUrl($url, $pathWhereToDownloadTheFile, $headers, /** You can pass an optional custom User-Agent as third argument ($customUserAgent) */); if (false !== $response) { // File downloaded successfully ! } else { // Error occurred ! } }
从 URL 获取文件扩展名
public function getFileExtensionFromUrl(string $url) { $fileExtension = $this->fileDownloader->getFileExtensionFromUrl($url); if (null === $fileExtension) { // Error occurred ! } }
下载 Symfony StreamedResponse
use Symfony\Component\HttpFoundation\StreamedResponse; // ... public function downloadStreamedResponse() { $streamedResponse = new StreamedResponse(); // ... $fileName = 'someFileName.csv'; return $this->fileDownloader->downloadStreamedResponse($streamedResponse, $fileName); }
许可
本包采用 MIT 许可证。请参阅包中的完整许可证。
查看 资源文档/LICENSE