FileUploader
, (*1)
An easily configurable file uploader for uploading files via a web form, (*2)
Features
- Set accepted file types
- Block unwanted file types
- Set maximum file size
- Option to auto create upload directories if they do not exist
- Option to overwrite files
- Option to automatically create unique filenames
- Filename sanitisation
Installing
The easiest way to install is via Composer:, (*3)
composer require craigh/file-uploader
, (*4)
or you can add craigh/file-uploader
to your composer.json file and run composer update
, (*5)
Note: The FileUploader requires symfony/HttpFoundation
, so if you are not using composer you will need to
make sure you have this library in your include path., (*6)
Usage
The FileUploader requires a Symfony\Component\HttpFoundation\File\UploadedFile
object. You can easily retrieve an instance by passing the $_FILES['Your_field_name'] variable into the getUploadedFile() method
on the Humps\FileUploader\File
class:, (*7)
$file = Humps\FileUploader\File::getUploadedFile($_FILES['file']);
, (*8)
This can then be passed in to the FileUploader:, (*9)
$uploader = new Humps\FileUploader\FileUploader($file);
, (*10)
You can then upload the file as follows:, (*11)
$uploader->upload();
, (*12)
Options
uploadDir(string)
Sets the upload path. It will also append any required '/' if it is not set, so both 'path/to/dir' and 'path/to/dir/' will work (defaults to current directory), (*13)
$uploader->uploadDir('path/to/dir');
, (*14)
overwrite(boolean)
Set to true to allow overwriting of files with the same name (default: false), (*15)
$uploader->overwrite(true);
, (*16)
allowedMimeTypes(array)
Pass in an array of allowed mime types, everything else will be blocked. When empty all file types will be allowed unless
explicitly blocked., (*17)
$uploader->allowedMimeTypes(['image/jpeg,'image/png', 'image/gif']);
, (*18)
blockedMimeTypes(array)
You can also block file types if you prefer. Pass in an array of mime types you want to block, (*19)
$uploader->blockedMimeTypes(['application/x-msdownload']);
, (*20)
maxFileSize($size, $unit)
The maximum file size you want to allow, expects size to be a number and unit to be either:
- B - Byte
- KB - Kilobyte
- MB - Megabyte, (*21)
$uploader->maxFileSize(5, 'MB');
, (*22)
You can also use the words BYTE, BYTES, KILOBYTE, KILOBYTES, MEGABYTE or MEGABYTES if you prefer:, (*23)
$uploader->maxFileSize(1, 'MEGABYTE');
, (*24)
createDirs(bool)
If set to true this will recursively create any specified directories if they do not exist (default: false), (*25)
$uploader->createDirs(true);
, (*26)
makeFilenameUnique(bool)
If set to true this will make the filename unique by appending a _{number} to the end., (*27)
$uploader->makeFilenameUnique(true);
, (*28)
filename(string)
By default the filename will be a sanitised version of the uploaded filename. Use this method if you want to set your own filename., (*29)
$uploader->filename('myFile.txt');
, (*30)
Note: When using this method the filename will not be sanatised, if you want to sanatise the filename you can use the sanitizeFilename() method., (*31)
sanitizeFilename()
Sanitises the given filename by removing any non alpha numeric characters and replacing any spaces with an underscore. You will only need to call this if you want to set your own filenames using the filename() method, otherwise this method is called automatically. You should also be aware that this call will need to be made after you set your filename:, (*32)
$uploader->filename('my%$crazy@filename.txt')->sanitizeFilename();
upload()
Uploads the file and returns the upload path., (*33)
$uploadPath = $uploader->upload();
, (*34)
upload() is an alias of move(), so you can also use the move() method if you feel it's wording is more appropriate:, (*35)
$uploadPath = $uploader->move();
, (*36)
Chaining
All methods above can be applied in a chain for a clean syntax:, (*37)
use Humps\FileUploader\File;
use Humps\FileUploader\FileUploader;
$file = File::getUploadedFile($_FILE['file']);
$uploader = new FileUploader($file);
$uploader->uploadPath->('files')->overwrite(true)->upload();
or even, (*38)
use Humps\FileUploader\File;
use Humps\FileUploader\FileUploader;
$file = File::getUploadedFile($_FILE['file']);
$uploader = (new FileUploader($file))->upload();
Config by Extending the FileUploader Class
For a cleaner way to configure you uploads you can extend the FileUploader class which will give you access to the protected
variables, e.g.:, (*39)
use Humps\FileUploader\FileUploader;
class ImageUploader extends FileUploader{
protected $allowedMimeTypes = [
'image/jpeg',
'image/png',
'image/gif'
];
protected $maxFileSize = 5e+6; // In bytes (this is 5MB or 5000000 bytes)
protected $makeFilenameUnique = true;
protected $createDirIfNotExists = true;
}
This can then be used as follows:, (*40)
use Humps\FileUploader\File;
$image = File::getUploadedFile($_FILE['image']);
$uploader = new ImageUploader($image);
$image->upload();
The following variables are protected and so can be set by child classes:, (*41)
protected $uploadDir; // Upload directory
protected $allowedMimeTypes = []; // Only allow these file to be uploaded
protected $blockedMimeTypes = []; // Don't allow these files to be uploaded
protected $maxFileSize = 1000000; // In bytes
protected $makeFilenameUnique = false; // Make the filename unique if two files have the same name
protected $overwrite = false; // Allow overwriting of files with the same name
protected $createDirs = false; // Allow the automatic creation of any upload directories
A note on the examples
If you want to run the examples then you will need to change the include path to point to your autoloader or include the FileUploader classes manually., (*42)