The Uploader class (src/Uploader.php
) provides a set of method that you can use to upload, delete, list or download files, etc.
To create a new instance you have to pass an instance of Hazzard\Config\Repository
, and Intervention\Image\ImageManager
.
use Hazzard\Filepicker\Uploader;
use Intervention\Image\ImageManager;
use Hazzard\Config\Repository as Config;
$uploader = new Uploader($config = new Config, new ImageManager);
Notice: These are just some of the more common methods, for the full list see
src/Uploader.php
.
Upload a file from the $_FILES
global variable or by passing an instance of UploadedFile. Retruns an instance of File if it succeeds or throws an UploadException exception if it fails.
Example:
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
try {
$file = $uploader->upload($_FILES['file']);
} catch (UploadException $e) {
echo 'Upload error: ' . $e->getMessage();
}
Returns an array of File instances.
Has two optional parameters: $offset = 0
to get the files from the specified offset and $limit = null
to limit the number of returned files.
Example:
$files = $uploader->get();
Returns the total number of uploaded files.
Returns a file download Response if the file is found or throws an FileNotFoundException exception.
Example:
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
try {
$response = $uploader->download('file.txt');
$response->send();
} catch (FileNotFoundException $e) {
echo 'File not found.';
}
For images you can pass a second parameter with the version name.
Returns an array of File instances that are the image versions files.
Example:
$files = $uploader->getImageVersions('image.jpg');
Delete a given file. Returns bool if the file is deleted or not and throws FileNotFoundException if the file is not found.
Example:
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
try {
if ($uploader->delete('file.txt')) {
echo 'File deleted.';
} else {
echo 'The file could not be deleted.';
}
} catch (FileNotFoundException $e) {
echo 'File not found.';
}
For images you can pass a second parameter with the version name.