First we have to configure the plugin with the specific configuration in the case Local Storage:
In the archive bootstrap.php
C: xampp htdocs [Projectfolder] config bootstrap.php
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem']
);
Put this block below the block with the use (Remember to add the use Burzum Filestorage Lib Storagemanager; library)
use Burzum\FileStorage\Lib\StorageManager;
use Cake\Cache\Cache;
use Cake\Console\ConsoleErrorHandler;
use Cake\Core\App;
use Cake\Core\Configure;
This line can be adjusted to your need (It contains the folder and the file will be saved).
'adapterOptions' => [TMP, true],
for (not necessarily this could be another depending on the need)
'adapterOptions' => [ROOT . DS . 'PicturesResources' . DS],
These are my tables in Mysql (Only two tables products and medias which stores the path to an image (media_types is not relevant to the problem))
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
quantity INT NOT NULL,
sold INT NOT NULL,
description VARCHAR(1000),
price DECIMAL(7,2) NOT NULL,
old_price DECIMAL(7,2) NOT NULL,
visited INT NOT NULL,
status INT NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE media_types (
id INT AUTO_INCREMENT PRIMARY KEY,
name_media_type VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE medias (
id INT AUTO_INCREMENT PRIMARY KEY,
media_type_id INT NOT NULL,
product_id INT NOT NULL,
path VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME,
FOREIGN KEY media_type_key (media_type_id) REFERENCES media_types(id),
FOREIGN KEY product_key (product_id) REFERENCES products(id)
);
I executed: cake Bake all products and cake Bake all medias and the result was:
In Productstable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('products');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Medias', [
'className' => 'Medias',
'foreignKey' => 'product_id'
]);
}
I added 'classname' => 'Medias', (I don’t remember if it’s optional but I added and there were no problems).
The archive Mediastable.php is the same generated by Bake.
public function initialize(array $config)
{
parent::initialize($config);
$this->table('medias');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('MediaTypes', [
'foreignKey' => 'media_type_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
}
Man Upload method in Productscontroller.php
public function upload() {
if ($this->request->is('post')) {
$mediaTypeId = 1;
$productId = 2;
$path = $this->request->data['Media']['file']['tmp_name'];
$inserted = $this->Insert->insertMedia($mediaTypeId, $productId, $path);
//-------------------------------------------------------------------------
$stringSeparator = '_';
$storeName = 'StoreGYN';
$productName = 'TestProduct';
$saved = $this->UploadFile->saveFileLFS($stringSeparator, $storeName,
$productName);
if($inserted === true && $saved === true){
$this->Flash->set(__('Upload successful!'));
}
}
}
I put the method responsible for Store the file in a component (this is optional, but this method must exist)
public function saveFileLFS($stringSeparator, $storeName, $productName)
{
$key = $storeName . $stringSeparator . $productName . $stringSeparator .
$this->request->data['Media']['file']['name'];
if(StorageManager::adapter('Local')->write($key,
file_get_contents($this->request->data['Media']['file']['tmp_name']))){
return true;
}else
{
return false;
}
}
And I put the method in charge of Save image path (on file system) in table in a component also:
public function insertMedia($mediaTypeId, $productId, $path)
{
$media = TableRegistry::get('Medias')->newEntity();
$media->media_type_id = $mediaTypeId;
$media->product_id = $productId;
$media->path = $path;
if(TableRegistry::get('Medias')->save($media)){
return true;
}
else{
return false;
}
}
This is the template, Pay close attention to HTML element names, they have to be the same as the $this->request->data['Media']['file']['tmp_name'];
otherwise you won’t be able to access the data sent in the form (including the file).
<?php
echo $this->Form->create(null, array(
'type' => 'file'
));
echo $this->Form->file('Media.file');
echo $this->Form->error('file');
echo $this->Form->submit(__('Upload'));
echo $this->Form->end();
?>
Note: I am using XAMPP and Cakephp 3
OBS 2: I noticed that many people also can not use the plugin following the tutorial as this question Soen