Handling and downloading files for Phalcon projects. Allowed multiple files, filters etc.... (Currently under TDD)
- ability to download files to a dynamically created directory (by [Mahdi-Mohammadi](https://github.com/Mahdi-Mohammadi))
- it support Phalcon 2.0
- ability to use closure (anonimous function) for generate uploaded file name
- ability to delete files after downloading ($uploader->truncate())
- added validator (sizes, extensions, mime types, directory upload)
- added filters (sanitize filename pre save, hash filename)
- PSR-0, PSR-1, PSR-2, PSR-4 Standards
- PHP 5.4.x >
- Phalcon extension 1.3.x
First update your dependencies through composer. Add to your composer.json:
"require": {
"stanislav-web/phalcon-uploader": "1.*",
}
Then run to update dependency and autoloader
php composer.phar update
php composer.phar install
or just
php composer.phar require stanislav-web/phalcon-uploader dev-master
(Do not forget to include the composer autoloader)
Or manual require in your loader service
$loader->registerNamespaces([
'Uploader\Uploader' => 'path to src'
]);
You can create an injectable service
$di->set('uploader', '\Uploader\Uploader');
<?php
if($this->request->hasFiles() !== false) {
// get uploader service
$uploader = $this->di->get('uploader');
// setting up uloader rules
$uploader->setRules([
'directory' => '/files',
//or 'dynamic' => '/files/'.$part.'/'.$userId, // added v1.4-beta
]);
// or use constructor if you don't use service
$uploader = new \Uploader\Uploader(([
'directory' => '/files',
//or 'dynamic' => '/files/'.$part.'/'.$userId, // added v1.4-beta
]);
}
<?php
if($this->request->hasFiles() !== false) {
// get uploader service or \Uploader\Uploader
$uploader = $this->di->get('uploader');
// setting up uloader rules
$uploader->setRules([
'directory' => '/files',
//or 'dynamic' => '/files/'.$part.'/'.$userId, // added v1.4-beta
'minsize' => 1000, // bytes
'maxsize' => 1000000,// bytes
'mimes' => [ // any allowed mime types
'image/gif',
'image/jpeg',
'image/png',
],
'extensions' => [ // any allowed extensions
'gif',
'jpeg',
'jpg',
'png',
],
'sanitize' => true // escape file & translate to latin
'hash' => 'md5' // save file as hash (default md5) you can use ANY function to handle filename
]);
}
<?php
if($this->request->hasFiles() !== false) {
// get uploader service or \Uploader\Uploader
$uploader = $this->di->get('uploader');
// setting up uloader rules
$uploader->setRules([
'directory' => '/files',
//or 'dynamic' => '/files/'.$part.'/'.$userId, // added v1.4-beta
'minsize' => 1000, // bytes
'maxsize' => 1000000,// bytes
'mimes' => [ // any allowed mime types
'image/gif',
'image/jpeg',
'image/png',
],
'extensions' => [ // any allowed extensions
'gif',
'jpeg',
'jpg',
'png',
],
'sanitize' => true
'hash' => 'md5'
]);
if($uploader->isValid() === true) {
$uploader->move(); // upload files array result
$uploader->getInfo() // var dump to see upload files
}
else {
$uploader->getErrors(); // var_dump errors
}
}
// you always can remove all files uploaded by one iteration
$uploader->truncate(); // added 1.1
Also available in /phpunit directory. Run command to start
php build/phpunit.phar --configuration phpunit.xml.dist --coverage-text
Read logs from phpunit/log
##Issues