Magento 2 – How to Copy File from One Folder to Another

magento2

I want to copy pdf file from one folder

i.e /pub/media/xyz to another /pub/media/abc programmatically.
And create target folder if does not exist.

Best Answer

I used the function copyFile() from Magento\Framework\Filesystem;

 use Magento\Framework\Filesystem;
  public function __construct(
        Filesystem $backendFile
    ) {
        $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    }

 $result = $this->mediaDirectory->copyFile(
            $this->getPath(self::TMP_SUBDIR, $filename),
            $this->getPath($subdirToSave, $filename)
        );