PHP code example of enjoys / upload
1. Go to this page and download the library: Download enjoys/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/ */
enjoys / upload example snippets
use Psr\Http\Message\ServerRequestInterface;
/** @var Psr\Http\Message\UploadedFileInterface $uploadedFile */
/** @var League\Flysystem\Filesystem $filesystem */
$file = new Enjoys\Upload\UploadProcessing($uploadedFile, $filesystem);
try {
$file->upload();
}catch (\Exception $e){
// handle exception
}
/** @var Enjoys\Upload\UploadProcessing $file */
/** @var Enjoys\Upload\RuleInterface $rule */
// ... set rule before called $file->upload()
$file->addRule($rule);
$file->upload();
$rule = new Enjoys\Upload\Rule\Extension();
$rule->allow('png');
// or
$rule->allow('png, jpg');
// or
$rule->allow(['png','jpg']);
$rule = new Enjoys\Upload\Rule\Size();
$rule->setMaxSize(10*1024*1024)
->setMinSize(1*1024*1024); // in bytes
$rule = new Enjoys\Upload\Rule\MediaType();
$rule->allow('image/*')
->allow('application/pdf')
// ...
;
/** @var Enjoys\Upload\UploadProcessing $file */
$file->setFilename('name');
/** @var Enjoys\Upload\UploadProcessing $file */
/** @var Enjoys\Upload\RuleInterface $rule */
$file->addRule($rule);
/** @var Enjoys\Upload\UploadProcessing $file */
/** @var Enjoys\Upload\RuleInterface[] $rules */
$file->addRules($rules);
/** @var Enjoys\Upload\UploadProcessing $file */
$file->upload(); // $file->upload('sub_directory');
/** @var Enjoys\Upload\UploadProcessing $file */
$file->getTargetPath(); // return null or string
/** @var Enjoys\Upload\UploadProcessing $file */
$file->getFilesystem();
/** @var Enjoys\Upload\UploadProcessing $file */
$file->getUploadedFile();
/** @var Enjoys\Upload\UploadProcessing $file */
$file->getFileInfo();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getFilename();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getOriginalFilename();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getFilenameWithoutExtension();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getExtension();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getExtensionWithDot();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getSize();
/** @var Enjoys\Upload\FileInfo $fileInfo */
$fileInfo->getMediaType();