Here is code for upload the image from admin side in magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
use Magento\Framework\App\Filesystem\DirectoryList; public function __construct( \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory, ) { $this->_fileUploaderFactory = $fileUploaderFactory; } public function uploadImage(){ $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']); $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(true); $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA) ->getAbsolutePath('{moduleName}/'); $result = $uploader->save($path); } |