How to get public folder absolute path in Shopware 6?

Viewed 1067

I'm trying to get a public folder path in shopware 6 in order to create a directory inside using the filesystem. Does anyone have an idea about it?

2 Answers

You could have a look at the service

shopware.filesystem.public which is defined in

vendor/shopware/core/Framework/DependencyInjection/filesystem.xml as

   <service class="League\Flysystem\FilesystemInterface" id="shopware.filesystem.public" public="true">
        <factory service="Shopware\Core\Framework\Adapter\Filesystem\FilesystemFactory" method="factory"/>
        <argument>%shopware.filesystem.public%</argument>
    </service>

When you inject this service, you can call

$publicFileSystem->createDir('folder')

to create your folder.

That does not exactly answer the question on how to get the path - but you usually don't need that, as you woult not interact directly with the filesystem, but via Flysystem classes instead.

If you really need the path, you could try something like

$publicFileSystem->getMetaData('.')['path']

This is untested by me and as written before, you might just not need the path. Also keep in mind, that the underlying filesystem might not even be a local storage.

Related