ExpressionEngine Docs

CP/FilePicker Service

Simple Example

Controlling user uploads can be tricky. To help ease the process, the file picker service can be used to create simple file upload UIs:

$filepicker = ee('CP/FilePicker')->make();

This creates a file picker object for all upload directories that the user has access to. It creates up the required modal html and injects the correct javascript into the output of the page.

Now we need to create a link to actually pop the file picker modal:

$link = $filepicker->getLink('Click me!');

And we need to show this link to the user:


Tip: If you aren’t further manipulating the $link object, you can chain the methods to generate an HTML string in a one liner:

$link = $filepicker->getLink('Click me!')->render();

That’s all there is to it! Clicking this link will pop open a file picker and allow the user to choose an existing file or upload a new file. Of course, this is not helpful unless that choice is populated somewhere.

Usually, the information we need is the file id that they chose. We can automatically populate this in a hidden form element. First we need to tell the link what id we will use for this hidden input:


Then we will add a hidden input, and we’ll put it near the link for clarity:

<input name="my-file" id="my-file" type="hidden">

You can also control this behavior more carefully by using a callback.

Link Methods

The link class has several utility methods to change the display and behavior of the file picker.


Set an html attribute on your link:

$link->setAttribute('id', 'my-upload');


Set an array of html attributes on your link:

  'id' => 'my-upload',
  'class' => 'myclass'


Change the text inside the link. Any html in this text will be encoded:

$link->setText('Choose a file!');


An alternative to setText for when you need to show other html elements inside your link:

$link->setHtml('Choose <b>one</b> file!');


Change the file picker list style to thumbnails:



Change the file picker list style back to a simple table. This is the default:



Disable the file picker’s filtering UI:



Re-enable the file picker’s filtering UI. This is the default:



Disable the file picker’s upload UI:



Re-enable the file picker’s upload UI. This is the default:


JavaScript Callbacks

The default javascript callbacks do not work for all cases. You can add your own using the $(...).FilePicker jQuery plugin. For this plugin to work you must still load the filepicker library in your controller. Then, simply call the plugin on your newly created link:

  callback: function(data, references) {
    // Close the modal

    // do work with data

The references will contain jQuery objects of the modal and the image tags and bound inputs, if any were specified. The data will contain a json representation of the selected file.