https://github.com/oscarotero/uploader
Basic php library to upload files
https://github.com/oscarotero/uploader
Last synced: 2 months ago
JSON representation
Basic php library to upload files
- Host: GitHub
- URL: https://github.com/oscarotero/uploader
- Owner: oscarotero
- License: mit
- Created: 2015-03-09T17:24:58.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-07T17:46:27.000Z (almost 9 years ago)
- Last Synced: 2025-01-31T13:11:37.572Z (3 months ago)
- Language: PHP
- Size: 16.6 KB
- Stars: 7
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# uploader
Basic php library to manage uploaded files
[](https://scrutinizer-ci.com/g/oscarotero/uploader/?branch=master)
Created by Oscar Otero
## Basic usage
```php
//Init a Uploader instance:
$uploader = new Uploader\Uploader('/base/path/to/uploads');//Save an uploaded file:
$file = $uploader($_FILES['my-file']);//Save a psr7 UploadedFileInterface instance
$file = $uploader($uploadedFile);//Save a file from an url
$file = $uploader('http://example.com/files/file1.jpg');//Save from base64 value
$file = $uploader('data:image/png;base64,...');
```## Advanced usage
```php
//Init a Uploader instance:
$uploader = new Uploader\Uploader('/base/path/to/uploads');//Add some configuration
$uploader
->setPrefix(function () {
return uniqid();
})
->setDirectory('images');//Assign an input
$imageUpload = $uploader->with($_FILES['my-image']);//Save
$imageUpload->save();//Get the relative path
echo $imageUpload->getDestination();
```## API
The following methods configure how the uploaded file will be saved
Setter | Getter | Description
-------|--------|------------
`setPrefix(string|Closure)` | `getPrefix()` | Custom filename prefix.
`setOverwrite(boolean|Closure)` | `getOverwrite()` | Whether or not overwrite existing files
`setDestination(boolean|Closure)` | `getDestination(bool $absolute = false)` | The destination file. If `$absolute` is `true`, returns the path with the cwd
`setDirectory(string|Closure)` | `getDirectory()` | To change only the directory of the destination
`setFilename(string|Closure)` | `getFilename()` | To change only the filename of the destination
`setExtension(string|Closure)` | `getExtension()` | To change only the file extension of the destination
`setCwd(string|Closure)` | `getCwd()` | To change the base path of the destination
`setCreateDir(boolean|Closure)` | `getCreateDir()` | Whether or not create the destination directory if it does not existExample:
```php
$uploader = new Uploader\Uploader(__DIR__.'/my-uploads');$upload = $uploader
->with($_FILES['my-file'])
->setPrefix(uniqid())
->setOverwrite(true)
->setCreateDir(true)
->setDirectory('files')
->setExtension(function ($upload) {
return strtolower($upload->getExtension());
});try {
$upload->save();echo 'The file has been saved in '.$upload->getDestination();
} catch (Exception $e) {
echo $e->getMessage();
}
```