In this page:
When trying to upload files in PHP, the developer have to access the global array
$_FILES
which can lead to a mess in some use cases. Also, it is difficult to use the global constant in case of multiple file uploads. More than that, there are security concerns when using the array directly for uploading user files.
The framework has a utility class that can be used to handle file uploads using mimimum amount of effort and code. The name of the class is
Uploader
and its part of the namespace
webfiori\framework
.
Uploader
The class
Uploader
is a utility class which is used to handle file uploads in PHP in a simple way. It has all necessary tools to handle one file upload or multiple files upload. The class will help in achieving the following:
$_FILES
to upload files.
Uploader
Usually, the most common use case is to upload a file to the server. This use case usually has the following steps:
Uploader
.
Uploader::upload()
to initiate upload process.
action
of the form to be the URL of the API.
method
of the form to
post
.
enctype
of the form to
multipart/form-data
Partial Backend Code (Shows Uploader initialization only):
Code
$uploader = new Uploader();
//sets upload path
$uploader->setUploadDir('\home\files\sys-uploads');
//allow pdf and word files
$uploader->addExts(['doc','docx','pdf']);
//sets the name of the location where files are kept in front-end
//This usually is the value of the attribute 'name' in case of HTML input element.
$uploader->setAssociatedFileName('files-input');
//start upload process. Replace if already uploaded.
$uploader->upload(true);
//shows a JSON string that shows upload status.
echo $uploader;
Assuming that a route to our upload API is created and the route is
apis/upload-files
, The front end code would be like the following:
Code
use webfiori\framework\ui\WebPage;
use webfiori\ui\HTMLNode;
use webfiori\ui\Input;
class UploadView extends WebPage {
__construct(){
$form = $this->insert('form');
//assume that upload API exist in the URL apis\upload-files
$form->setAttributes([
'method'=>'post'
'action'=>'apis/upload-files'
'enctype'=>'multipart/form-data'
]);
$filesInput = new Input('file');
$form->addChild($filesInput);
//The attribute name must be set to the value which
// is passed to the method Uploader::setAssociatedFileName()
$file->setName('files-input')
$submit = new Input('submit');
$form->addChild($submit);
}
}
return __NAMESPACE__
Note: It is possible to skip setting the associated file name in server by adding a hidden input field with
name="file"
andvalue
set to the name of the actual input which is used to upload files.
The code which is used to upload multiple files at one call is the same as the one which is used to upload one file. The diffrence is that we have to set the attribute 'multiple' of the file input element and set the name to a syntax which looks like an array as following:
Code
$file->setAttribute('multiple')
$file->setName('files-input[]')
The method
Uploader::upload()
will return an associative array that contains upload status of uploaded files. In addition to getting files information as an associative array, it is possible to get uploaded files information as objects. The method
Uploader::uploadAsFileObj()
will upload the files and return an array that contains objects of type
UploadedFile
. Also, it is possible to get uploaded files information even after the upload process is finished using the method
Uploader::getFiles()
.
Next: Sending Emails
Previous: Themes