cancel
Showing results for 
Search instead for 
Did you mean: 

magento2 : How to copy a file programmatically in Controller

Highlighted

magento2 : How to copy a file programmatically in Controller

I have a controller execute() function in which I would like to make a file copy from a directory to another.

In Magento 1.9 I was using :

 

Mage::helper('core/file_storage_database')->copyFile($_OriginalPath, $_NewPath);

How to perform that in Magento2?

 

I tried :

use Magento\Framework\App\Filesystem\DirectoryList;

    protected $_FileDB;
    protected $mediaDirectory;

        public function __construct(
            \Magento\Framework\Filesystem $filesystem,
            \Magento\MediaStorage\Helper\File\Storage\Database $FileDB

                )
        {
            parent::__construct($context);
            $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
            $this->_FileDB = $FileDB;
        }

and

$this->_FileDB->copyFile($_oldPath,$_NewPath);

but no file copied.Thank you for your help