|
| 1 | +## Laravel File Manager |
| 2 | + |
| 3 | +### Installation: |
| 4 | +``` |
| 5 | +composer require serjik/laravel-file-manger |
| 6 | +``` |
| 7 | + |
| 8 | +You must add the service provider to `config/app.php` |
| 9 | +``` php |
| 10 | +'providers' => [ |
| 11 | + // for laravel 5.8 and below |
| 12 | + \Serjik\FileManager\FileManagerServiceProvider::class, |
| 13 | +]; |
| 14 | +``` |
| 15 | + |
| 16 | +**Publish your config file and migrations** |
| 17 | + |
| 18 | +``` |
| 19 | +php artisan vendor:publish |
| 20 | +``` |
| 21 | +<hr> |
| 22 | + |
| 23 | +### Config: |
| 24 | +> config/filemanager.php |
| 25 | +``` php |
| 26 | +return [ |
| 27 | + "type" => "default", |
| 28 | + |
| 29 | + "types" => [ |
| 30 | + "default" => [ |
| 31 | + "provider" => \Serjik\FileManager\Types\File::class, |
| 32 | + "path" => "default_files/test/", |
| 33 | + "private" => false, |
| 34 | + "date_time_prefix" => true, |
| 35 | + "use_file_name_to_upload" => false, |
| 36 | + "secret" => "ashkdsjka#sdkdjfsj22188455$$#$%dsDFsdf", |
| 37 | + "download_link_expire" => 160, // minutes |
| 38 | + ], |
| 39 | +// we add another types |
| 40 | +// "image" => [ |
| 41 | +// // |
| 42 | +// ], |
| 43 | + ], |
| 44 | +]; |
| 45 | +``` |
| 46 | + |
| 47 | +### Config Parameters |
| 48 | + |
| 49 | + |
| 50 | +| name | type | description | |
| 51 | +|---------------|--------------|---------------------------| |
| 52 | +| provider | `string (class name)`| provider class name, must be extended of `Serjik\FileManager\BaseType` | |
| 53 | +|path | `string` | file upload path | |
| 54 | +|private | `boolean` | is private or no if is `true` so upload file in storage folder else if is `false` so upload file in public folder | |
| 55 | +|date_time_prefix|`boolean` | if is `true` so upload file with `/{year}/{month}/{day}` prefix| |
| 56 | +|use_file_name_to_upload| `boolean`| if is `true` we use of the file original name else we generate a random name| |
| 57 | +|secret |`string` | secret key for generate download link and download file| |
| 58 | +|download_link_expire|`boolean`|generated download link expire time| |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Lets start to use: |
| 63 | + |
| 64 | +#### Upload a file: |
| 65 | +```php |
| 66 | +$file = request()->file('filename'); |
| 67 | +$upload = File::upload($file); |
| 68 | + |
| 69 | +// get file uploaded path |
| 70 | +$filePath = $upload->getFilePath(); |
| 71 | + |
| 72 | +// get file name |
| 73 | +$fileName = $upload->getName(); |
| 74 | +``` |
| 75 | + |
| 76 | +#### You can use of this methods: |
| 77 | + |
| 78 | +| method |description | |
| 79 | +|--------------------------------------|---------------------------------------------| |
| 80 | +| `useFileNameToUpload($status = true)`|if is `true` we use of the file original name else we generate a random name| |
| 81 | +|`getFile($name = null)` |get file by name and return a `\Serjik\FileManager\Models\File`| |
| 82 | +| `setPath($path)` |set file upload path | |
| 83 | +| `getUploadPath()` |get upload path | |
| 84 | +| `dateTimePrefix($value = true)` |if is `true` so upload file with `/{year}/{month}/{day}` prefix| |
| 85 | +| `setName(string $name)` |set file name | |
| 86 | +| `setFormat(string $format)` |set format for file upload | |
| 87 | +| `isPrivate()` | if you call this so upload file in storage folder and your you don't have permission to access this file| |
| 88 | +| `isPublic()` |if you call this so upload file in public folder and has access to this file| |
| 89 | + |
| 90 | + |
| 91 | +### Examples: |
| 92 | +```php |
| 93 | +$file = request()->file('filename'); |
| 94 | +$upload = File::setName('your specific name') |
| 95 | + ->isPrivate() |
| 96 | + ->setFormat('png') |
| 97 | + ->dateTimePrefix() |
| 98 | + ->upload($file); |
| 99 | +// get file uploaded path => if is public you can use it for download |
| 100 | +dd($upload->getFilePath()); |
| 101 | +``` |
| 102 | +```php |
| 103 | +$file = File::getFile("file uploaded name"); |
| 104 | +$file->name; |
| 105 | +$file->path; |
| 106 | +$file->type; // config file selected type |
| 107 | +$file->isPrivate; |
| 108 | +$file->isPublic; |
| 109 | +$file->generateLink(); |
| 110 | + |
| 111 | +// return response download |
| 112 | +// $file->download(); |
| 113 | +``` |
0 commit comments