Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Upload & Thumbnail


File Upload

Uploading file is very simple. You can set various preferences, restricting the type, extension and size of the file etc.

File upload process includes:

i. Html Form to allow user to select a file to upload it. You may have a look at Cygnite Form Builder for creating file upload form.

ii. Validate the file size and type before uploading, if doesn't match as expected then throw exception.

iii. Once form submitted file will upload to the destination folder and show success message to the user.

Creating the Upload Form

You can use Form Builder to build your form. The default location of your form classes is /src/Apps/Form/ directory.

 use Cygnite\FormBuilder\Form;
 use Cygnite\Common\UrlManager\Url;

  $this->open('users', [
                   'method' => 'post',
                   'action' => Url::sitePath('home/upload'),
                   'enctype' => 'multipart/form-data'
            ->addElement('label', 'File Upload', [
                       'class' => 'col-sm-2 control-label',
                       'style' => 'width:37.667%;'
            ->addElement('file', 'document' , [
                       'class' => 'form-control'])

Paste the above code snippet inside buildForm() method of your form class. It will generate a form with file input element as below.

 <form name='users' method='post' action='' 
enctype='multipart/form-data' > <label for='File Upload' class='col-sm-2 control-label' style='width:37.667%;' >File Upload</label> <input name='document' class='form-control' type='file' /> </form>

For more information about generating a form, read more Form Builder documentation.

Let us assume, you have created a form using Form Builder and now let us upload a file.

Setting Root Directory

Sometimes you may wish to store files in different location instead of default location. Such cases you can set root path where your upload directory exists. By default the root directory is your installed project directory.

 use Cygnite\Common\File\Upload\Upload;

 $upload = Upload::process( function($file) {
    return $file;

Setting Destination Folder For Upload

You can set the upload directory path where files will get uploaded. You may provide the destination path as below.

 $upload->save(["destination"=> "public/upload"]); //path: var/www/cygnite/public/upload/

Validating file before uploading

Sometime you may want to set the valid files type, file extensions, and size. Below example how to configure valid extension, size etc.

 $upload->ext = ['PNG', 'JPEG']; // valid file extension is PNG, JPEG only

 $upload->setUploadSize('32092'); // file size should match


 $upload->size = '32092'; // file size for validation

Uploading A File

You should add a route entry to your controller's action, where your form is submitted. Inside your action you can use below code to upload a file. Make sure you are adding the namespace Cygnite\Common\File\Upload\Upload top of your controller class.

 use Cygnite\Common\File\Upload\Upload;

 $response = Upload::process( function($upload)
     $upload->file = 'document'; // input element name
     $upload->ext = ["JPG"]; // file extension validation
     $upload->size = '32092';  // file size for validation

     $response = [];
     $config = ["destination"=> "public/upload/", "fileName"=>"file-new-name"];
     if ( $upload->save($config) ) {
         // Upload Success
         $response = ['status' => true, 'msg' => 'Profile Photo Uploaded Successfully!'];
     } else {
         // Catch errors here
         $response = ['status' => false, 'msg' => implode(', \n', $upload->getError())];

     return $response;


The Upload Folder & Permission

You'll need a destination folder for your uploaded images. Inside your project installed folder you can find public folder, you can create a folder called upload and set the permissions to 777.

Creating Thumbnail Image

Creating a thumbnail or cropping a uploaded image is just simple using Thumbnail library. It is bundled with cygnite framework package.

Example Usage:

 $image = new \Cygnite\Common\File\Thumbnail\Image();

 $image->setRootDir(CYGNITE_BASE); // set root path
 //Your image location anywhere inside the framework  root directory

 $image->directory = 'public/uploads'; // Original image location directory

 $image->fixedWidth  = 100; // width of the thumbnail image
 $image->fixedHeight = 100; // height of the thumbnail image
 $image->thumbPath = 'thumbnails/'; // destination path for thumbnail image
 $image->thumbName = ''; // new name for thumbnail image
 $image->resize(); // Crop the image and create a thumbnail image into specified location

Make sure your destination path has file permissions as 777.

Setting Root Directory

Some cases you may wish to use different root path where your files are located. User setRootDir method to notify the image library where it should search for the image. Your image files may be located in uploads directory.

 $image->setRootDir(); // If you don't specify system will use getcwd() as root path


 $image->setRootDir(CYGNITE_BASE); // cygnite root base path

Downloading File

You can download file from the specific path using file downloader.

 $file = new \Cygnite\Common\File\File();

 // By default system will use cygnite installation directory if you don't specify root directory


It allows you to download various types of files as jpeg, jpg, gif, png, pdf, docx, zip, xls, video formats etc.

Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.