1

I use following code,

public function execute()
{
    //do your custom stuff here
    $fileName = 'file name for download here';
    $this->fileFactory->create(
        $fileName,
        null, //content here. it can be null and set later 
        base dir of the file to download here
        'application/octet-stream', //content type here
        content lenght here...can be null
    );
 }

What would be the base dir for this file?

/magento/pub/media/downloadable/files/links/l/e/learn-magento.jpg

6
  • r u using linux or windows.. ? Commented Apr 5, 2018 at 11:45
  • Server is unix! Commented Apr 5, 2018 at 11:45
  • and you are using apache2? means your /var/www/html folder Commented Apr 5, 2018 at 11:46
  • Yes I am using apache2 Commented Apr 5, 2018 at 11:48
  • Magento throws error: "unknown Directory Type" Commented Apr 5, 2018 at 11:51

2 Answers 2

1

For Getting Base url For Directory you can follow this.

    protected $_filesystem;

    public function __construct(
        \Magento\Framework\Filesystem $filesystem,
        Action\Context $context
    ){
        $this->_filesystem = $filesystem;
        parent::__construct($context);
    }

    public function getBaseDir() {
        $path = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('your_path');
        return $path;
    }

Pass Your Directory in getAbsolutePath('Here')

2

By ObjectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');

echo $rootPath  =  $directory->getRoot();

By Factory Method

protected $_dir;
...

public function __construct(
    ...
    \Magento\Framework\Filesystem\DirectoryList $dir,
    ...        
) {
    ...
    $this->_dir = $dir;
    ...
}
4
  • can you show me how to call that? Commented Apr 5, 2018 at 11:56
  • I have updated my answer. you look over there. Commented Apr 5, 2018 at 11:57
  • @L.Klmn It's working for you.? Commented Apr 5, 2018 at 13:01
  • I think yes, the file is located here: /var/www/medienshop-dev/magento/pub/media/downloadable/files/links/l/e/learn-magento.jpg and the value of baseDir is the same. However it still says "Unknown Directory Type". Commented Apr 5, 2018 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.