2

I already sent the file from phtml to controller and in the controller, I need to save that file into a directory here are HTML and PHP code.

<form class="myformdynamic" onsubmit="return Validate()" method="post" action="<?php echo $this->getUrl('helloworld/index/result');?>"  enctype="multipart/form-data">
    <input type="file" id="profileAdd" name="profileAdd"><span class="s"id="sfile"></span>
    <button type="submit" id="btnsave" class="action submit primary">
        <span >Save</span>
    </button>
</form>

Here is my code Result.php controller in another file,

<?php

namespace Inchoo\Helloworld\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Result extends Action
{
    public function execute()
    {
        $fileName=['profileAdd']['name'];
        echo $fileName;
        //name of the file is printed which i uploaded from form
    }
}

1 Answer 1

3

You can upload using at \Magento\MediaStorage\Model\File\UploaderFactory.

protected $_mediaDirectory;
protected $_fileUploaderFactory;
 
public function __construct(
    Context $context,        
    \Magento\Framework\Filesystem $filesystem,
    \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
) {
    $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
    $this->_fileUploaderFactory = $fileUploaderFactory;
    parent::__construct($context);
}

public function execute()
{
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    try{
        $target = $this->_mediaDirectory->getAbsolutePath('mycustomfolder/');        
        /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
        $uploader = $this->_fileUploaderFactory->create(['fileId' => 'profileAdd']); //Since in this example the input controller name is 'profileAdd', it shoud be used here
        /** Allowed extension types */
        $uploader->setAllowedExtensions(['jpg', 'pdf', 'doc', 'png', 'zip', 'doc']);
        /** rename file name if already exists */
        $uploader->setAllowRenameFiles(true);
        /** upload file in folder "mycustomfolder" */
        $result = $uploader->save($target);
        if ($result['file']) {
            $this->messageManager->addSuccess(__('File has been successfully uploaded')); 
        }
    } catch (\Exception $e) {
        $this->messageManager->addError($e->getMessage());
    }
    return $this->resultRedirectFactory->create()->setPath(
        '*/*/upload', ['_secure'=>$this->getRequest()->isSecure()]
    );            
}
10
  • sir it is still showing some error Fatal error: Uncaught TypeError: Argument 2 passed to Inchoo\Helloworld\Controller\Index\Result::__construct() must be an instance of Magento\Framework\Filesystem, none given, called in C:\xampp\htdocs\magento\var\generation\Inchoo\Helloworld\Controller\Index\Result\Interceptor.php on line 14 and defined in Commented Oct 18, 2017 at 7:43
  • 1
    delete all file from var/genearete and recompile
    – Amit Bera
    Commented Oct 18, 2017 at 7:53
  • sir after this line some errors are showing $target = $this->_mediaDirectory->getAbsolutePath('mycustomfolder/'); what should i write in in place of fileId=>'file' Commented Oct 18, 2017 at 8:18
  • thanku sir problem is solved Commented Oct 18, 2017 at 9:30
  • would you please also tell me how should fetch image fromthat uploaded folder Commented Oct 18, 2017 at 9:36

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.