1. Go to this page and download the library: Download josbeir/cakephp-filesystem 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/ */
namespace App\Controller;
use Josbeir\Filesystem\FilesystemAwareTrait;
class MyController extends AppController {
use FilesystemAwareTrait;
public function upload()
{
$fileEntity = $this->getFilesystem('myfs')->upload($this->request->getData('upload'));
debug($fileEntity);
}
}
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Josbeir\Filesystem\FileEntityInterface;
class MyFileEntity extends Entity implements FileEntityInterface
{
public function getPath() : string
{
return $this->path;
}
public function setPath(string $path) : FileEntityInterface
{
$this->set('path', $path);
return $this;
}
}
$entity = $this->Posts->get(1);
$fileEntity = $this->getFilesystem()->upload(TMP . 'myfile.png', [
'formatter' => 'Entity', // formatter to use
'data' => $entity // data to pass to the formatter
]);
// Upload a file
// Will fire Filesystem.beforeUpload and Filesystem.afterUpload
$this->getFilesystem()->upload($data, $config);
// Upload multiple files and returns a FileEntityCollection
// Will fire Filesystem.beforeUpload and Filesystem.afterUpload (after each file upload)
$this->getFilesystem()->uploadMany($files, $config);
// Copy an entity
// Will fire Filesystem.beforeCopy and Filesystem.afterCopy
$this->getFilesystem()->copy($entity, $config);
// Rename an entity
// Will fire Filesystem.beforeRename and Filesystem.afterRename
$this->getFilesystem()->rename($entity, $config);
// Delete an entity from the FS
// Will fire Filesystem.beforeDelete and Filesystem.afterDelete
$this->getFilesystem()->delete($entity);
// Check if a file entity exists on the FS
$this->getFilesystem()->exists($entity);
// Get Flysystem FS instance
$this->getFilesystem()->getDisk();
// Get Flysystem adatapter
$this->getFilesystem()->getAdapter();
// Set the formatter class name to be used
$this->getFilesystem()->setFormatter($name);
// Return a new formatter instance
$this->getFilesystem()->newFormatter($filename, $config);
// Reset formatter and adapter to default configuration
$this->getFilesystem()->reset();