PHP code example of cohensive / upload

1. Go to this page and download the library: Download cohensive/upload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cohensive / upload example snippets


// Laravel 4
php artisan config:publish cohensive/upload
// Laravel 5
php artisan vendor:publish

'providers' => array(
    //...
    'Cohensive\Upload\UploadServiceProvider',
    //...
)

'aliases' => array(
    //...
    'Upload' => 'Cohensive\Upload\Facades\Laravel\Upload'
    //...
)

// Create new Upload instance.
// Both params are optional arrays. Description of allowed array items below.
$upload = Upload::make($rules, $options);
if ($upload->passes()) {
    // If file is valid, save it in the uploads (set in options) directory.
    $file = $upload->save();
    // $file - is an instance of Cohensive\Upload\File class with various file-related attributes and methods.
} else {
    // Get array of errors - simple list of failed validation rules.
    $upload->getErrors();
}

$validator = new \Cohensive\Upload\Validator([
    'maxSize' => 2097152 // Set maximum allowed filesize to 2MB (set in bytes)
]);

$sanitizer = new \Cohensive\Upload\Sanitizer\AbstractSanitizer($sanitizerClass, $sanitizerMethod);

$fileFactory = new \Cohensive\Upload\FileHandlerFactory();

$options = [...]; // An array of options for Upload, such as upload directory etc.
$upload = new \Cohensive\Upload\Upload($validator, $sanitizer, $fileFactory, $options);

$rules = [...]; // An array of validation rules. Optional.
if ($upload->passes($rules)) {
    // If file is valid, save it in the uploads (set in options) directory.
    $file = $upload->save();
} else {
    // Get array of errors - simple list of failed validation rules.
    $errors = $validator->getErrors();
}

$file->move($destination);           // Move file to another directory. Returns boolean.
$file->delete();                     // Delete file.
$file->getFileinfo();                // Returns array containing file information.
$file->getFilepath();                // Returns path to the file.
$file->getMimetype();                // Returns file MIME.
$file->getExtension();               // Returns file extension.
$file->isExists();                   // Checks if files exists. Returns boolean.

    'uploadDir'   => 'uploads/',     // Folder where all uploaded files will be saved to.
    'tmpDir'      => 'uploads/tmp/', // Folder to keep files temporary for operations.
    'param'       => 'file',         // Parameter to access the file on.
    'name'        => '',             // Set new filename. Blank to use original name.
    'nameLength'  => 32,             // Set maximum length of the name. Will be cut if longer.
    'prefix'      => '',             // Add prefix to the filename..
    'suffix'      => '',             // Add suffix to the filename.
    'case'        => '',             // Convert file name to the case: 'lower', 'upper' or falsy to keep original.
    'overwrite'   => false,          // If file already exists, overwrite it.
    'autoRename'  => true,           // In case if file with the same name exists append counter to the new file.
    'randomize'   => false,          // Generate random filename. Boolean or integer for string length. Default length is 10.
    'sanitize'    => true            // Sanitize filename - remove whitespaces and convert utf8 to ascii.

    'minSize'   => 0,        // Minimum filesize.
    'maxSize'   => 10485760, // Maximum filesize: 10MB.
    'maxWidth'  => 0,        // Maximum image width if file is an image.
    'maxHeight' => 0,        // Maximum image height if file is an image.
    'minWidth'  => 0,        // Minimum image width if file is an image.
    'minHeight' => 0,        // Minimum image height if file is an image.
    'width'     => [],       // Image must have exact width (use array to set multiple).
    'height'    => [],       // Image must have exact height (use array to set multiple).
    'whiteExt'  => ['jpg', 'jpeg', 'gif', 'png'], // Array of allowed extensions.
    'blackExt'  => []                             // Array of disallowed extensions.