src/Subjects/FileUploadTrait.php
public function uploadFile($filename)
    {

        // trim the leading /, if available
        $trimmedFilename = ltrim($filename, '/');
        $mediaDir = ltrim($this->getMediaDir(), '/');
        $imagesFileDir = ltrim($this->getImagesFileDir(), '/');

        // prepare source/target filename
        $sourceFilename = sprintf('%s/%s', $imagesFileDir, $trimmedFilename);
        $targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename);

        // query whether or not the image file to be imported is available
        if (!$this->getFilesystemAdapter()->isFile($sourceFilename)) {
            throw new \Exception(sprintf('Media file %s not available', $sourceFilename));
        }

        // prepare the target filename, if necessary
        $newTargetFilename = $this->getNewFileName($targetFilename);
        $targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename);

        // make sure, the target directory exists
        if (!$this->getFilesystemAdapter()->isDir($targetDirectory = dirname($targetFilename))) {
            $this->getFilesystemAdapter()->mkdir($targetDirectory, 0755);
        }

        // copy the image to the target directory
        $this->getFilesystemAdapter()->copy($sourceFilename, $targetFilename);

        // return the new target filename
        return str_replace($mediaDir, '', $targetFilename);
    }
public function uploadFile($filename)
    {

        // trim the leading /, if available
        $trimmedFilename = ltrim($filename, '/');
        $mediaDir = ltrim($this->getMediaDir(), '/');
        $imagesFileDir = ltrim($this->getImagesFileDir(), '/');

        // prepare source/target filename
        $sourceFilename = sprintf('%s/%s', $imagesFileDir, $trimmedFilename);
        $targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename);

        // query whether or not the image file to be imported is available
        if (!$this->getFilesystemAdapter()->isFile($sourceFilename)) {
            throw new \Exception(sprintf('Media file %s not available', $sourceFilename));
        }

        // prepare the target filename, if necessary
        $newTargetFilename = $this->getNewFileName($targetFilename);
        $targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename);

        // make sure, the target directory exists
        if (!$this->getFilesystemAdapter()->isDir($targetDirectory = dirname($targetFilename))) {
            $this->getFilesystemAdapter()->mkdir($targetDirectory, 0755);
        }

        // copy the image to the target directory
        $this->getFilesystemAdapter()->copy($sourceFilename, $targetFilename);

        // return the new target filename
        return str_replace($mediaDir, '', $targetFilename);
    }
Upload's the file with the passed name to the Magento
media directory. If the file already exists, the will
be given a new name that will be returned.

@param string $filename The name of the file to be uploaded

@return string The name of the uploaded file
@throws \Exception Is thrown, if the file with the passed name is not available