How to use Simple File Uploader
This object provides a simple use of File Upload with few lines of code. When you upload a file, it is saved in temp folder. After the form save you could retrieve all file send and move it in your folder
Step 1: create instance
Create an instance of Simple File Uploader. In our example is named : simpleFileUpload
Next, set the parameters : - input name: It's like the name of your input element in a form; - directory: to store your file; - session: sessionManagerInterface to start the session automatically.
Others parameters are mandatory. The most important: - OnlyOneFile: If you want to send only 1 file (many by default). This delete the file contain in the destination folder to store only one. Note: it's not a graphic parameter; - noTemporary : You can activate it to don't send the file in the temporary folder. The file is saved in the destination folder; - fileExtensions: Limit the extension name of file send; - sizeLimit: The max size of file in byte; - minSizeLimit: The min size of file in byte; - fileName: To rename the file with a name; - multiple: authorize the user to send many file in 1 time (by drag & drop or explorer). Note: It's only a graphic parameter.
Add in your controller
Add your instance in your controller
/** * * @var SimpleFileUploaderWidget */ public $fileUpload;
In Mouf add to your controller the fileUpload instance
Add in your form
Add the code in your form to display the button :
<form action="your_method" method="post"> <?php $this->fileUpload->toHtml(); ?> <input type="submit" name="submit" value="Send" /> </form>
Retrieve your files
In the save action, you could retrieve the file. The parameter is the name of hidden input passed in inputName (above).
Move all file in your folder:
If you want to change the destination folder (example: to add an id)
There is a function to get the list of files in temporary folder:
In listener afterUpload
Your controller (class) must implements FileUploaderOnUploadInterface. you will add 2 functions beforeUpload and afterUpload. In afterUpload you could move your file with the function moveFileWithoutSave. The first parameter is ALWAYS $result['targetFolder'].
Found a typo? Something is wrong in this documentation? Just fork and edit it!