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?

Edit 3:

I tried :

use Magento\Framework\App\Filesystem\DirectoryList;

    protected $_FileDB;

        public function __construct(
            \Magento\MediaStorage\Helper\File\Storage\Database $FileDB

            $this->_FileDB = $FileDB;



and still no file copied...

2 Answers 2


You can use cp() function of class Magento\Framework\Filesystem\Io\File


protected $_fileSystem;

public function __construct(
    \Magento\Framework\Filesystem\Io\File $filesystemIo,
) {
    $this->filesystemIo = $filesystemIo;

$filePath  = '/var/www/html/var/import/order_status.csv'
$$copyFileFullPath  = '/var/www/html/var/import/readfiles/order_status.csv'

$filePath = $this->_fileSystemIo->cp($filePath, $copyFileFullPath);


I think this will work for you. For DB storage. Magento\MediaStorage\Helper\File\Storage\Database

public function copyFile($oldName, $newName)
    if ($this->checkDbUsage()) {

It basically calls the copyFile() of this class \Magento\MediaStorage\Model\File\Storage\Database which in turn call this class Magento\MediaStorage\Model\ResourceModel\File\Storage\Database.

  • thanks for your answer : I get a Zend_Db_Statement_Exception exception (cf. my edit)
    – Alexglvr
    Commented Apr 12, 2017 at 12:07
  • That function needs only 2 params. copyFile($oldFilePath, $newFilePath). Why you are sending 4 params? Open the file and check the function. Commented Apr 12, 2017 at 13:10
  • I made the correction and get a 'magentoDB.media_storage_file_storage does not exist
    – Alexglvr
    Commented Apr 12, 2017 at 15:03
  • any idea what goes wrong?
    – Alexglvr
    Commented Apr 13, 2017 at 10:19
  • Let me try it. Will post after checking. Commented Apr 13, 2017 at 10:29

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.