Dhaval Kanojiya
Dhaval Kanojiya

Reputation: 97

How to get public folder absolute path in Shopware 6?

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?

Upvotes: 6

Views: 3388

Answers (3)

Julien Thrun
Julien Thrun

Reputation: 1

Instead of trying to get the public path through the file-system, you can also simply inject the according config from it via:

'%shopware.filesystem.public.config.root%'

This will give you the path to the public-directory.

Upvotes: 0

mnaczenski
mnaczenski

Reputation: 1626

Maybe not exactly bulletproof, but most of the time you can also use getcwd();

https://www.php.net/manual/en/function.getcwd.php

I mostly use it for writing debug-files like error_log(print_r($data, true)."\n", 3, getcwd().'/error.log');

Upvotes: 2

Alex
Alex

Reputation: 35118

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.

Upvotes: 5

Related Questions