Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 62e7a1a

Browse files
committed
Added composer support and added namespace usage
1 parent 2f0fc4b commit 62e7a1a

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ This class is for providing downloads of files out of PHP, for example if you wa
99

1010
# Usage
1111

12+
The examples assume, that you have included the namespace:
13+
```php
14+
use Apfelbox\FileDownload\FileDownload;
15+
```
16+
1217

1318
## Create a download for a file on your file system
1419
```php

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "apfelbox/php-file-download",
3+
"type": "library",
4+
"description": "A library to help with creating downloads for files in PHP",
5+
"keywords": ["download"],
6+
"homepage": "https://github.com/apfelbox/PHP-File-Download",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Jannik Zschiesche",
11+
"email": "[email protected]",
12+
"homepage": "http://apfelbox.net"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.3",
17+
"skyzyx/mimetypes": "~1.1"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Apfelbox\\FileDownload\\": "src/"
22+
}
23+
}
24+
}

FileDownload.php renamed to src/FileDownload.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @license MIT
88
*/
99

10+
namespace Apfelbox\FileDownload;
11+
12+
use Skyzyx\Components\Mimetypes\Mimetypes;
1013

1114
/**
1215
* Provides a simple way to create file downloads in PHP
@@ -27,13 +30,13 @@ class FileDownload
2730
*
2831
* @param resource $filePointer
2932
*
30-
* @throws InvalidArgumentException
33+
* @throws \InvalidArgumentException
3134
*/
3235
public function __construct ($filePointer)
3336
{
3437
if (!is_resource($filePointer))
3538
{
36-
throw new InvalidArgumentException("You must pass a file pointer to the ctor");
39+
throw new \InvalidArgumentException("You must pass a file pointer to the ctor");
3740
}
3841

3942
$this->filePointer = $filePointer;
@@ -81,20 +84,11 @@ public function sendDownload ($filename)
8184
*/
8285
private function getMimeType ($fileName)
8386
{
84-
switch (pathinfo($fileName, PATHINFO_EXTENSION))
85-
{
86-
case "pdf": return "application/pdf";
87-
case "exe": return "application/octet-stream";
88-
case "zip": return "application/zip";
89-
case "doc": return "application/msword";
90-
case "xls": return "application/vnd.ms-excel";
91-
case "ppt": return "application/vnd.ms-powerpoint";
92-
case "gif": return "image/gif";
93-
case "png": return "image/png";
94-
case "jpeg":
95-
case "jpg": return "image/jpg";
96-
default: return "application/force-download";
97-
}
87+
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
88+
$mimeTypeHelper = Mimetypes::getInstance();
89+
$mimeType = $mimeTypeHelper->fromExtension($fileExtension);
90+
91+
return !is_null($mimeType) ? $mimeType : "application/force-download";
9892
}
9993

10094

0 commit comments

Comments
 (0)