|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Provides the possibility to easily create file downloads in PHP |
| 4 | + * |
| 5 | + * @author Jannik Zschiesche <[email protected]> |
| 6 | + * @author Karel Wintersky <[email protected]> |
| 7 | + * @version 1.0 |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Arris\Toolkit; |
| 12 | + |
| 13 | +use InvalidArgumentException; |
| 14 | +use RuntimeException; |
| 15 | +use Skyzyx\Components\Mimetypes\Mimetypes; |
| 16 | + |
| 17 | +class FileDownload implements FileDownloadInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * The pointer to the file to download |
| 21 | + * |
| 22 | + * @var resource |
| 23 | + */ |
| 24 | + private $filePointer; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $fileName; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a new file download |
| 33 | + * |
| 34 | + * @param resource $filePointer |
| 35 | + * |
| 36 | + * @throws InvalidArgumentException |
| 37 | + */ |
| 38 | + public function __construct($filePointer, string $filePath) |
| 39 | + { |
| 40 | + if (!is_resource($filePointer)) { |
| 41 | + throw new InvalidArgumentException("You must pass a file pointer to the constructor"); |
| 42 | + } |
| 43 | + |
| 44 | + $this->filePointer = $filePointer; |
| 45 | + $this->fileName = pathinfo($filePath, PATHINFO_BASENAME); |
| 46 | + } |
| 47 | + |
| 48 | + public function sendDownload(string $filename = '', bool $forceDownload = true) |
| 49 | + { |
| 50 | + if (headers_sent()) { |
| 51 | + throw new RuntimeException("Cannot send file to the browser, since the headers were already sent."); |
| 52 | + } |
| 53 | + |
| 54 | + if (empty($filename)) { |
| 55 | + $filename = $this->fileName; |
| 56 | + } |
| 57 | + |
| 58 | + header("Pragma: public"); |
| 59 | + header("Expires: 0"); |
| 60 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
| 61 | + header("Cache-Control: private", false); |
| 62 | + header("Content-Type: {$this->getMimeType($filename)}"); |
| 63 | + |
| 64 | + if ($forceDownload) { |
| 65 | + header("Content-Disposition: attachment; filename=\"{$filename}\";"); |
| 66 | + } else { |
| 67 | + header("Content-Disposition: filename=\"{$filename}\";"); |
| 68 | + } |
| 69 | + |
| 70 | + header("Content-Transfer-Encoding: binary"); |
| 71 | + header("Content-Length: {$this->getFileSize()}"); |
| 72 | + |
| 73 | + @ob_clean(); |
| 74 | + |
| 75 | + rewind($this->filePointer); |
| 76 | + fpassthru($this->filePointer); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the mime type of a file name |
| 81 | + * |
| 82 | + * @param string $fileName |
| 83 | + * |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + private function getMimeType(string $fileName): string |
| 87 | + { |
| 88 | + $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION); |
| 89 | + $mimeTypeHelper = Mimetypes::getInstance(); |
| 90 | + $mimeType = $mimeTypeHelper->fromExtension($fileExtension); |
| 91 | + |
| 92 | + return !is_null($mimeType) ? $mimeType : "application/force-download"; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the file size of the file |
| 97 | + * |
| 98 | + * @return int |
| 99 | + */ |
| 100 | + public function getFileSize(): int |
| 101 | + { |
| 102 | + $stat = fstat($this->filePointer); |
| 103 | + if ($stat === false) { |
| 104 | + throw new RuntimeException("Get File size error"); |
| 105 | + } |
| 106 | + |
| 107 | + return $stat['size']; |
| 108 | + } |
| 109 | + |
| 110 | + public static function createFromFilePath(string $filePath): FileDownloadInterface |
| 111 | + { |
| 112 | + if (!is_file($filePath)) { |
| 113 | + throw new InvalidArgumentException("File does not exist"); |
| 114 | + } else if (!is_readable($filePath)) { |
| 115 | + throw new InvalidArgumentException("File to download is not readable"); |
| 116 | + } |
| 117 | + |
| 118 | + return new static(fopen($filePath, "rb"), $filePath); |
| 119 | + } |
| 120 | + |
| 121 | + public static function createFromString(string $content): FileDownloadInterface |
| 122 | + { |
| 123 | + $file = tmpfile(); |
| 124 | + fwrite($file, $content); |
| 125 | + |
| 126 | + return new static($file, ''); |
| 127 | + } |
| 128 | + |
| 129 | + public static function createFromResource($fileResource): FileDownloadInterface |
| 130 | + { |
| 131 | + $meta_data = stream_get_meta_data($fileResource); |
| 132 | + $filename = $meta_data["uri"]; |
| 133 | + |
| 134 | + return new static($fileResource, $filename); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +# -eof- |
0 commit comments