Filesystem best practices

I also needed something like that recently. Only solution I found to get basename and dirname was using:

\Magento\Framework\Filesystem\Io\File

protected function someFunction()
{
    /** @var \Magento\Framework\Filesystem\Io\File $fileSystemIo **/
    $fileInfo = $this->fileSystemIo->getPathInfo('<absolutePath>');
    $basename = $fileInfo['basename'] 
    $dirname = $fileInfo['dirname'];
}

Before that I tried using Magento\Framework\Filesystem\Directory\Write and getDriver() with no success. With them you can get pretty much everything but not the basename.


Luckily git lets us see when dirname and basename were forbidden, the reason is clearly, "Added files"

Looking at the issue for the ECG project you can see closed issues such as something bad in file_exists? #33, Error Functions #26, something bad in this functions? #17, Context / Explanation for the Rules #12, The use of function iconv() is forbidden #14 which would make me think that the initial list of forbidden functions wasn't given too much consideration, and magento are probably amenable to changing the forbidden list.

Searching the m2 codebase shows ~= 78 results for basename, a mix of variables and the code actually calling basename, including my favourite.

I think if I were you I'd post an issue on github and ask zlik if he still thinks they belong there or if M2 provides a wrapper


You can use object of SplFileInfo() class may be it will work.

$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename())

may be it will work.

you can also refer this url.