dev-master
9999999-dev http://samsonos.com/
Open Software License (OSL) v 3.0
The Requires
The Development Requires
by Vitaly Iegorov
dev-omelchenko
dev-omelchenko http://samsonos.com/
Open Software License (OSL) v 3.0
The Requires
by Vitaly Iegorov
Wallogit.com
2017 © Pedro Peláez
To use this module in your project you must add dependency in your composer.json:, (*2)
"require": {
"samsonos/php_upload": "*"
},
After doing composer install or composer update you will get all classes and functions of Upload module., (*3)
Configuration for Upload module is not necessary, but you can use it if you want to create unique file name or upload dir name for uploading files.
If you will not use configuration, your upload catalog will be called 'upload' and file name will be random generated., (*4)
Thanks to SamsonPHP module/service configuration you can create simple config class:, (*5)
class UploadConfig extends \samson\core\Config
{
// Callback function for creating upload dir
public $uploadDirHandler = 'uploadDirHandler';
// Callback function for creating file name
public $fileNameHandler = 'uploadNameHandler';
}
The main feature of current module is asynchronous file uploading. Look at the example., (*6)
First of all you need to create html container with your file input:, (*7)
<p>
<input type="hidden" class="__action" value="<?php url_base('catalog/upload'); ?>">
<input type="hidden" class="__file_size" value="50000000">
<input class="__example_upload" type="file" name="uploadFile">
</p>
Also you can add following feature blocks : * Container with class "__progress_text" will show you percents of file uploading. * Paragraph tag inside container with class "__progress_bar" will create animation as progress bar of uploading., (*8)
Remember, that you must add some css styles for this blocks to use them as you want., (*9)
If you are using input with class different from "__upload", you need to handle you inputs:, (*10)
// Cache file input
var file = s('.__example_upload');
// Bind upload event
uploadFileHandler(file, {
// Handle event before file uploading
start: function () {
alert('Uploading started');
},
// Handle event after upload finishing
response: function (response) {
alert('Uploading finished');
}
});
The second parameter in function uploadFileHandler(file, options) is not necessary, you can use it for creating something special while file is uploading., (*11)
Server action in our HTML example is controller 'catalog/upload'. Let's create this function:, (*12)
function catalog_async_upload()
{
// Parameter for callback functions
$parameter = 5;
// Create AJAX response array
$json = array('status' => 0);
// Create object for uploading file to server
$upload = new \samson\upload\Upload(array('png','jpg'), $parameter);
if ($upload->upload($filePath, $fileName, $realName) {
$json['status'] = 1;
// Add full path to uploaded file into AJAX response
$json['filePath'] = $filePath;
// Add uploaded file name into AJAX response
$json['fileName'] = $fileName;
// Add real file name (user's file name) into AJAX response
$json['realName'] = $realName;
}
return $json;
}
To create file upload you need to create class \samson\upload\Upload, constructor of which can have three parameters., (*13)
Method that directly create uploading called upload(& $filePath = '', & $fileName = '', & $realName = '').
You can get main file information using parameters of this method., (*14)
Also we can create our callback functions that are defined in configuration class:, (*15)
/**
* External handler for defining upload catalog
*/
function uploadDirHandler($parameter)
{
return 'upload/catalog'.$parameter;
}
/**
*
*/
function uploadNameHandler($parameter, $extension)
{
return 'item'.$parameter.$extension;
}
As you see, we have parameter $extension in our name handler function. This parameter is always defined as last parameter of this callback function.
In our case after created callback functions, upload catalog will be called 'upload/catalog5' and file name will be like 'item5'.
Don't forget, that in current example all uploading files will have simple name (maybe different extensions), so you must come up with some logic for using upload file name handler correctly., (*16)
Developed by SamsonOS., (*17)
Open Software License (OSL) v 3.0
Open Software License (OSL) v 3.0