0

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

                )
        {
            parent::__construct($context);
            $this->_FileDB = $FileDB;
        }

and

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

and still no file copied...

2 Answers 2

3

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

vendor/magento/framework/Filesystem/Io/File.php

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);

 ...
0

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

public function copyFile($oldName, $newName)
{
    if ($this->checkDbUsage()) {
        $this->getStorageDatabaseModel()->copyFile(
            $this->_removeAbsPathFromFileName($oldName),
            $this->_removeAbsPathFromFileName($newName)
        );
    }
}

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.

12
  • 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.