First we have to configure the plugin with the specific configuration in the Local Storage case:
In the bootstrap.php
C: \ xampp \ htdocs \ [ProjectFolder] \ config \ bootstrap.php
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem']
);
Place this block below the block with use (Remember to add the Burzum \ FileStorage \ Lib \ StorageManager;
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 needs (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 means that 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 ran: 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've added 'className' = > 'Medias', (I do not remember if it's optional but I added and there were no problems).
The file MediasTable.php is the same as that 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'
]);
}
My 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 Storing the file into 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 responsible for Save image path (in the file system) in the 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 the name of HTML elements , they must be the same as $this->request->data['Media']['file']['tmp_name'];
otherwise you will not 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();
?>
I'm using XAMPP and CakePHP 3
NOTE 2: I noticed that a lot of people are also not able to use the plugin following the
17.08.2015 / 20:03