Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change save folder in burzum/cakephp-file-storage plugin

I'm using a plugin to save files in CakePHP 3: burzum/cakephp-file-storage, everything works perfectly but now I need to change the directory where file is being saved dynamically (through the directory where the file is saved by parameter for example), how can I do this?

Path actual:

[htdocs]\[AppName]\tmp[FileExtension.png]

It is possibly stay with the following structure for example:

[htdocs]\[AppName-Images]\Products[FileExtension.png]

PHP Code:

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; 
} 
} 

Github Link

like image 232
Wellington Avelino Avatar asked Feb 25 '26 09:02

Wellington Avelino


1 Answers

The reason it ends up in TMP is that this is the default configuration that comes with the plugin. It goes to TMP because this is the only place besides logs that should be writeable in a proper application setup and the plugin should work out of the box. Maybe I'll change this in a future release (4.0) so that you have to configure it to use it, to make people aware of it.

For the local adapter change it like this:

StorageManager::config('Local', [
    'adapterOptions' => [ROOT . DS . 'file_storage' . DS], // Your base path here
    'adapterClass' => '\Gaufrette\Adapter\Local',
    'class' => '\Gaufrette\Filesystem'
]);

This will override the default. Actually this is already documented and explained here.

I recommend you to take a look at the 1.1.0 branch (currently release is 1.1.0-RC1). Some people already use it and I'm happy to get feedback about it. The whole way paths and file names are generated has been completely rewritten and abstracted into a set of classes called "path builders".

like image 176
floriank Avatar answered Feb 26 '26 21:02

floriank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!