If you use Filepicker in a application with users you may want to protect the files directory from users that are not logged in.
First create a .htaccess
file inside the files directory containing:
Deny from all
Next edit uploader/index.php
and enable the php_download option:
$config['php_download'] = true;
Now in before any other event listeners register one with these events: file.get, file.download, file.delete and crop.before:
$handler->on(array('file.get', 'file.download', 'file.delete', 'crop.before'), function(Event $e) {
// If the user is Unauthorized, then:
$e->abort('Unauthorized');
$e->preventDefault(); // Prevent other events to be fired.
});
To check if the user is authorized you may include a library from your app, call a function, check if a specific session is set, etc.
Learn more about the abort and preventDefault events.
To sort the files you can simply register a listener for the files.get event and add use usort for example to sort the files.
$handler->on('files.get', function(array &$files) {
usort($files, function($a, $b) {
if ($a->getMTime() === $b->getMTime()) {
return 0;
}
// ">" - newest | "<" - oldest
return ($a->getMTime() > $b->getMTime()) ? -1 : 1;
});
});
$handler->on('files.get', function(array &$files) {
usort($files, function($a, $b) {
if ($a->getSize () === $b->getSize ()) {
return 0;
}
// ">" - descending | "<" - ascending
return ($a->getSize () > $b->getSize ()) ? -1 : 1;
});
});
For alphabetically order see the sorting_order option.
If you wish to send data to the server you can do that by using the formData option. You can either pass an object of parameters or a function that returns an object.
Example 1:
$('#filepicker').filePicker({
// ...
formData: {
paramA: 'value1',
paramB: 'value2',
},
});
Example 2:
$('#filepicker').filePicker({
// ...
formData: function() {
// Grab the value from an input.
var val = $('#myinput').value();
// Return the object.
return {
paramA: 'value 1',
paramB: val
}
},
});
On the server side in any of the handler events you can access the data with $_POST
(or $_GET
when loading the files):
$handler->on('upload.before', function(Event $e) {
$valueA = $_POST['paramA'];
$valueB = $_POST['paramB'];
});
$handler->on('file.get', function(Event $e) {
$valueA = $_GET['paramA'];
$valueB = $_GET['paramB'];
});