VIP*_*ROY 0 media path absolute magento2
如何在Magento2中获取媒体文件的绝对路径?我写了一个函数来获取绝对路径,但它不起作用.
public function getAbsolutePath()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\Filesystem $filesystem */
$filesystem = $objectManager->get('Magento\Framework\Filesystem');
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
$mediaDirectory = $filesystem->getDirectoryWrite(Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$mediaPath = $mediaDirectory->getAbsolutePath();
return $mediaPath;
}
Run Code Online (Sandbox Code Playgroud)
应用程序/ autoloader.php
包含:
/**
* Shortcut constant for the root directory
*/
define('BP', dirname(__DIR__));
Run Code Online (Sandbox Code Playgroud)
人们可以这样做:
$mediaPath = BP.'/pub/media/';
Run Code Online (Sandbox Code Playgroud)
也就是说,Magento\Framework\Filesystem通过依赖注入是我的首选方法