1. Go to this page and download the library: Download allanmcarvalho/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/ */
allanmcarvalho / upload example snippets
Plugin::load('Upload', ['bootstrap' => true]);
// in App\Model\Table\ExamplesTable.php
class ExamplesTable extends Table
{
...
public function initialize(array $config)
{
parent::initialize($config);
$this->addBehavior('Upload.Upload', [
'file1' => [],
'file2' => []
]);
}
...
// in App\Model\Table\ExamplesTable.php
$this->addBehavior('Upload.Upload', [
'file1' => [
'image' => [
'crop' => [
'width' => 600 // Height will be the same
],
'format' => 'png',
'quality' => 75,
'resize' => [
'height' => 750, // width will be automatically calculated
],
'thumbnails' => [
[
'label' => 'thumb1',
'resize' => [
'height' => 750,
],
'watermark' => false // Disables watermark for this item
],
[
'label' => 'thumb2',
'resize' => [
'min_size' => 750,
],
'watermark' => [
'opacity' => 60, // 60% of opacity
'position' => 'top', // center top position
'path' => WWW_ROOT . 'img' . DS . 'watermark2.png',
],
'crop' => [
'width' => 600 // Height will be the same
]
]
],
'watermark' => [
'ignore_default' => true, //do not insert watermark on default
'opacity' => 10, // 10% of opacity
'path' => WWW_ROOT . 'img' . DS . 'watermark.png',
'position' => 'bottom-right'
]
]
]
]);
// in App\Template\Controller\add.ctp
...
<?= $this->Form->create($example, ['type' => 'file'])
// in App\Controller\ExampleController.php
...
public function deleteFiles($id)
{
$this->request->allowMethod(['post', 'delete']);
$example = $this->Examples->get($id);
if ($this->Examples->deleteFiles($example)) {
$this->Flash->success(__('The files has been deleted.'));
} else {
$this->Flash->error(__('The files could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
...
// in App\Controller\ExampleController.php
...
public function deleteFiles($id)
{
$this->request->allowMethod(['post', 'delete']);
$example = $this->Examples->get($id);
if ($this->Examples->deleteFiles($example, ['file1'])) {
$this->Flash->success(__('The files has been deleted.'));
} else {
$this->Flash->error(__('The files could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
...
// in App/Model/Table/ExampleTable.php
// Contain files validations
public function validationDefault(Validator $validator)
{
$validator->setProvider('upload', \Upload\Validation\UploadValidation::class);
$validator
->add('file1', 'isUnderPhpSizeLimit', [
'rule' => 'isUnderPhpSizeLimit',
'message' => 'Greater than the PHP size limit',
'provider' => 'upload'
]);
$validator
->add('file1', 'isUnderFormSizeLimit', [
'rule' => 'isUnderFormSizeLimit',
'message' => 'Greater than the FORM size limit',
'provider' => 'upload'
]);
$validator
->add('file1', 'isCompletedUpload', [
'rule' => 'isCompletedUpload',
'message' => 'Upload not completed',
'provider' => 'upload'
]);
$validator
->add('file1', 'isFileUpload', [
'rule' => 'isFileUpload',
'message' => 'File not uploaded',
'provider' => 'upload'
]);
$validator
->add('file1', 'isSuccessfulWrite', [
'rule' => 'isSuccessfulWrite',
'message' => 'Failed to write file',
'provider' => 'upload'
]);
$validator
->add('file1', 'isAboveMinSize', [
'rule' => ['isAboveMinSize', 2048],
'message' => 'Does not have the minimum
// in App/Model/Table/ExampleTable.php
// Contain image validations
public function validationDefault(Validator $validator)
{
$validator->setProvider('upload', \Upload\Validation\ImageValidation::class);
$validator
->add('file1', 'isAboveMinWidth', [
'rule' => ['isAboveMinWidth', 100],
'message' => 'Must have a wider width',
'provider' => 'upload'
]);
$validator
->add('file1', 'isBelowMaxWidth', [
'rule' => ['isBelowMaxWidth', 900],
'message' => 'Must have the shortest width',
'provider' => 'upload'
]);
$validator
->add('file1', 'isAboveMinHeight', [
'rule' => ['isAboveMinHeight', 100],
'message' => 'Must have a wider height',
'provider' => 'upload'
]);
$validator
->add('file1', 'isBelowMaxHeight', [
'rule' => ['isBelowMaxHeight', 900],
'message' => 'Must have the shortest height',
'provider' => 'upload'
]);
$validator
->add('file1', 'isThisWidth', [
'rule' => ['isThisWidth', 800],
'message' => 'Must have a width of 800px',
'provider' => 'upload'
]);
$validator
->add('file1', 'isThisHeight', [
'rule' => ['isThisHeight', 900],
'message' => 'Must have a height of 900px',
'provider' => 'upload'
]);
$validator
->add('file1', 'isThisWidthAndHeight', [
'rule' => ['isThisWidthAndHeight', 800, 900],
'message' => 'Must have a width of 800px and height of 900',
'provider' => 'upload'
]);
$validator
->add('file1', 'isThisAspectRatio', [
'rule' => ['isThisAspectRatio', 3, 4],
'message' => 'Wrong aspect ratio',
'provider' => 'upload'
]);
$validator
->add('file1', 'isThisExtension', [
'rule' => ['isThisExtension', ['jpg', 'png']],
'message' => 'Wrong image extension',
'last' => true,
'provider' => 'upload'
]);
}
// in App/Model/Table/ExampleTable.php
// Contains both validations
public function validationDefault(Validator $validator)
{
$validator->setProvider('upload', \Upload\Validation\DefaultValidation::class);
...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.