Hello. Tell me how to solve the problem in yii2 so that the downloaded files are stored on a separate deployed server. Those. web application is one server, and file server is another.

  • What operating system is used on these servers? - Zhukov Roman
  • @ZhukovRoman Ubuntu 04/14 - Roman

1 answer 1

On this issue you can help this component. It allows you to store files on remote servers.

To use it, you need to add the following to the application configuration:

return [ 'components' => [ 'fileStorage' => [ 'class' => 'yii2tech\filestorage\local\Storage', 'basePath' => '@webroot/files', 'baseUrl' => '@web/files', 'filePermission' => 0777, 'buckets' => [ 'tempFiles' => [ 'baseSubPath' => 'temp', 'fileSubDirTemplate' => '{^name}/{^^name}', ], 'imageFiles' => [ 'baseSubPath' => 'image', 'fileSubDirTemplate' => '{ext}/{^name}/{^^name}', ], ] ], // ... ], // ... ]; 

After which it can be used as follows:

 bucket = Yii::$app->fileStorage->getBucket('tempFiles'); $bucket->saveFileContent('foo.txt', 'Foo content'); // create file with content $bucket->deleteFile('foo.txt'); // deletes file from bucket $bucket->copyFileIn('/path/to/source/file.txt', 'file.txt'); // copy file into the bucket $bucket->copyFileOut('file.txt', '/path/to/destination/file.txt'); // copy file from the bucket var_dump($bucket->fileExists('file.txt')); // outputs `true` echo $bucket->getFileUrl('file.txt'); // outputs: 'http://domain.com/files/f/i/file.txt' 
  • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - HamSter
  • one
    @Elena if you thought about it for a moment, rather than automatically acting as a moderator, you would notice that the link leads to the main page of the githab library, which displays information from the readme file. Obviously, this order is unlikely to change in the near future, and if the author of the library changes or deletes the library description, then it will become irrelevant! - StalkAlex