1

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!

anonymous
3,7624 gold badges26 silver badges67 bronze badges
asked Jun 13, 2018 at 11:25

4 Answers 4

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/');
answered Jun 13, 2018 at 11:40
4
  • 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 Commented Jun 13, 2018 at 11:52
  • can you share your code please ? Commented Jun 13, 2018 at 11:54
  • the error should avoid using use Magento\Framework\App\Filesystem\DirectoryList; which described by Prashant. Commented Jun 13, 2018 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? Commented Jun 13, 2018 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

answered Jun 13, 2018 at 11:55
6
  • 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? Commented Jun 13, 2018 at 12:03
  • Please give permission to your folder Commented Jun 13, 2018 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/'; Commented Jun 13, 2018 at 12:06
  • can you please share your output => echo $mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images'); die; Commented Jun 13, 2018 at 12:11
  • C:/xampp/htdocs/magento/pub/media/images/ Commented Jun 13, 2018 at 12:14
0

Change this:

\Magento\Framework\App\Filesystem\DirectoryList $filesystem

to this:

\Magento\Framework\Filesystem $fileSystem

It will solve the problem.

answered Mar 31, 2022 at 9:03
0

Use \Magento\Framework\Filesystem $filesystem instead of \Magento\Framework\App\Filesystem\DirectoryList $filesystem.

Updated code should looks like this :

<?php
namespace Vendor\Module\Controller\Adminhtml\UpdateTable;
use Magento\Framework\App\Filesystem\DirectoryList;
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\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/');
 }
}
answered May 23, 2024 at 19:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.