1. Go to this page and download the library: Download nzo/file-downloader-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nzo / file-downloader-bundle example snippets
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');
}
}
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 !
}
}
public function getFileExtensionFromUrl(string $url)
{
$fileExtension = $this->fileDownloader->getFileExtensionFromUrl($url);
if (null === $fileExtension) {
// Error occurred !
}
}
use Symfony\Component\HttpFoundation\StreamedResponse;
// ...
public function downloadStreamedResponse()
{
$streamedResponse = new StreamedResponse();
// ...
$fileName = 'someFileName.csv';
return $this->fileDownloader->downloadStreamedResponse($streamedResponse, $fileName);
}