Enable your website users to select files using elFinder File Browser. You can also use this tool as a file picker module for tinyMCE editor.
Version 2.0 brings modern ESM support, removes jQuery dependency, eliminates global variables, and auto-injects CSS styles for a cleaner integration experience.
Looking for v1.x? If you need the legacy version with jQuery and global variables, see the v1.1.2 documentation on npm.
Coming soon
npm i -s elfinder-pickeryarn add elfinder-picker<script type="module">
import ElFinderPicker from 'https://cdn.jsdelivr.net/npm/elfinder-picker@2/dist/index.mjs';
// Use the picker
</script>import ElFinderPicker from 'elfinder-picker';
// Create a new instance (styles are auto-injected!)
const picker = new ElFinderPicker({
url: '/path/to/elfinder.html'
});
// Open the picker with a callback that receives the file object
picker.open((file) => {
console.log('Selected file:', file);
// file = { url: '...', name: '...', mime: 'image/jpeg' }
document.getElementById('myInput').value = file.url;
document.getElementById('fileName').textContent = file.name;
});
// Or open with an initial value (navigates to that file in elFinder)
const currentFile = document.getElementById('myInput').value;
picker.open((file) => {
document.getElementById('myInput').value = file.url;
}, currentFile);Create your picker instance and use it:
import ElFinderPicker from 'elfinder-picker';
const picker = new ElFinderPicker({ url: '/path/to/elfinder.html' });
// Open the picker and handle file selection
document.getElementById('selectFileBtn').addEventListener('click', () => {
picker.open((file) => {
console.log('Selected file:', file.url, file.name, file.mime);
document.getElementById('fileInput').value = file.url;
});
});Import and use the provided helper callback (minimal code required):
import { filePickerCallback } from 'elfinder-picker';
// Configure elFinder with the helper
$('#elfinder').elfinder({
url: '/path/to/elfinder/connector.php',
getFileCallback: filePickerCallback // That's it!
});The helper automatically handles everything for you:
- ✅ Detects if running in picker mode
- ✅ Sends file data (URL, name, MIME type) back to the picker
- ✅ Zero configuration needed
Restrict postMessage to your specific domain:
import { createFilePickerCallback } from 'elfinder-picker';
const callback = createFilePickerCallback({
origin: 'https://yourdomain.com' // Only allow messages to your domain
});
$('#elfinder').elfinder({
url: '/path/to/elfinder/connector.php',
getFileCallback: callback
});TinyMCE needs different response formats depending on which dialog is open. Use the MIME type to determine how to format the response:
import ElFinderPicker from 'elfinder-picker';
const picker = new ElFinderPicker({ url: '/path/to/elfinder.html' });
tinymce.init({
selector: '.text-editor',
height: 350,
plugins: ['link', 'image', 'media'],
file_picker_callback: (callback, value, meta) => {
picker.open((file) => {
// Format response based on TinyMCE dialog type
if (meta.filetype === 'image') {
callback(file.url, { alt: file.name });
} else if (meta.filetype === 'media') {
callback(file.url);
} else {
// For links/files
callback(file.url, { text: file.name, title: file.name });
}
});
},
});You can also use the file's MIME type to make decisions:
file_picker_callback: (callback, value, meta) => {
picker.open((file) => {
// Use MIME type to determine file category
if (file.mime.startsWith('image/')) {
callback(file.url, { alt: file.name });
} else if (file.mime.startsWith('video/') || file.mime.startsWith('audio/')) {
callback(file.url);
} else {
callback(file.url, { text: file.name, title: file.name });
}
});
}new ElFinderPicker(config)config.url(string): URL to your elFinder instance
Open the file picker modal
Parameters:
callback(Function): Called when a file is selected. Receives a file object:{ url: string, // Normalized file URL name: string, // File name mime: string // MIME type (e.g., 'image/jpeg', 'application/pdf') }
value(string, optional): Initial file path to navigate to in elFindermeta(Object, optional): Metadata for TinyMCE compatibility
Example:
// Basic usage
picker.open((file) => {
console.log(file.url, file.name, file.mime);
});
// With initial value (navigate to specific file)
picker.open((file) => {
document.getElementById('input').value = file.url;
}, '/uploads/images/current-file.jpg');
// Check file type
picker.open((file) => {
if (file.mime.startsWith('image/')) {
// Handle image
}
});Update picker configuration
config.url(string): URL to your elFinder instance
Close the picker modal
Remove the picker from DOM and clean up resources
Ready-to-use callback for elFinder's getFileCallback. Automatically sends file data (URL, name, MIME type) to the parent picker.
Parameters:
file(Object): File object from elFinderfm(Object): elFinder instanceoptions(Object): Optional configurationoptions.origin(string): Target origin for postMessage (default:'*')
Example:
import { filePickerCallback } from 'elfinder-picker';
$('#elfinder').elfinder({
url: '/connector.php',
getFileCallback: filePickerCallback
});Creates a configured file picker callback with custom origin for production security
Parameters:
options(Object): Configuration optionsoptions.origin(string): Target origin for postMessage
Returns: Function compatible with elFinder's getFileCallback
Example:
import { createFilePickerCallback } from 'elfinder-picker';
const callback = createFilePickerCallback({
origin: 'https://yourdomain.com'
});
$('#elfinder').elfinder({
url: '/connector.php',
getFileCallback: callback
});MIT