0

I have a controller that works and does what i need it to do if i manually add a path like: $mediaImages = '/magento/pub/media/images/';

But i cant get the DirectoryList injection to work, it returns undefined method?

<?php
  namespace Vendor\Module\Controller\Adminhtml\UpdateTable;

  class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_postFactory;
    protected $_filesystem;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $pageFactory,
    \NewcastleMotorcycles\AdminMenu\Model\PostFactory $postFactory,
    \Magento\Framework\App\Filesystem\DirectoryList $filesystem
    )
{
    $this->_pageFactory = $pageFactory;
    $this->_postFactory = $postFactory;
    $this->_filesystem = $filesystem;
    return parent::__construct($context);
}

public function execute()
  {
    $mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/');

  }
}

The error i get is:

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\App\Filesystem\DirectoryList::getDirectoryRead()

I'm obviously missing somtehing, any advise would be great!

0

Instead of using direct object manager, use It like

use Magento\Framework\App\Filesystem\DirectoryList;

protected $_filesystem;

public function __construct(
    \Magento\Framework\Filesystem $_filesystem,
)
{
    $this->_filesystem = $_filesystem;
}

Now you can media path by,

$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/');
  • I have tried with 'use' and get this error: Fatal error: Trait '[VENDOR][MODULE]\Controller\Adminhtml\UpdateTable\Magento\Framework\App\Filesystem\DirectoryList' not found in C:\xampp\htdocs\magento\app\code[VENDOR][MODULE]\Controller\Adminhtml\UpdateTable\Index.php on line 6 – Bossman Jun 13 '18 at 11:52
  • can you share your code please ? – user68116 Jun 13 '18 at 11:54
  • the error should avoid using use Magento\Framework\App\Filesystem\DirectoryList; which described by Prashant. – Amit Bera Jun 13 '18 at 11:55
  • I have added the 'use' correctly now and have it working, almost. I get Almost, i get Not allowed to load local resource: file:///C:/xampp/htdocs/magento/pub/media/images/thumb_slider2f.jpg Is their something i need to set with Magento? Like i say folder permissions are fine if i manually enter the path? – Bossman Jun 13 '18 at 12:05
0

Try this:

<?php
namespace Vendor\Module\Controller\Adminhtml\UpdateTable;

use Magento\Framework\App\Filesystem\DirectoryList;

class Index extends \Magento\Backend\App\Action
{
    protected $_pageFactory;
    protected $_postFactory;
    protected $_filesystem;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \NewcastleMotorcycles\AdminMenu\Model\PostFactory $postFactory,
        \Magento\Framework\Filesystem $fileSystem
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_postFactory = $postFactory;
        $this->_filesystem = $filesystem;
        return parent::__construct($context);
    }

    public function execute()
    {
        $mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images');

    }
}

After run command: php bin/magento setup:di:compile

  • Almost, i get Not allowed to load local resource: file:///C:/xampp/htdocs/magento/pub/media/images/thumb_slider2f.jpg Is their something i need to set with Magento? Like i say folder permissions are fine if i manually enter the path? – Bossman Jun 13 '18 at 12:03
  • Please give permission to your folder – Abdul Jun 13 '18 at 12:04
  • That's what i mean. The permissions are fine. This controller can write and read from that folder if i manually add the path like this: $mediaImages = '/magento/pub/media/images/'; – Bossman Jun 13 '18 at 12:06
  • can you please share your output => echo $mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images'); die; – Abdul Jun 13 '18 at 12:11
  • C:/xampp/htdocs/magento/pub/media/images/ – Bossman Jun 13 '18 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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