src/Subjects/FileUploadTrait.php
/**
     * 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
     */
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
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
public function uploadFile($filename)
    {
        $trimmedFilename = ltrim($filename, '/');
        $mediaDir = ltrim($this->getMediaDir(), '/');
        $imagesFileDir = ltrim($this->getImagesFileDir(), '/');
        $sourceFilename = sprintf('%s/%s', $imagesFileDir, $trimmedFilename);
        $targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename);
        if (!$this->getFilesystemAdapter()->isFile($sourceFilename)) {
            throw new \Exception(sprintf('Media file "%s" is not available', $sourceFilename));
        }
        if ($this->imageHasNotBeenMapped($filename)) {
            $newTargetFilename =  $this->getNewFileName($targetFilename);
            $targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename);
            if (!$this->getFilesystemAdapter()->isDir($targetDirectory = dirname($targetFilename))) {
                $this->getFilesystemAdapter()->mkdir($targetDirectory, 0755);
            }
            $this->getFilesystemAdapter()->copy($sourceFilename, $targetFilename);
            $this->addImageMapping($filename, str_replace($mediaDir, '', $targetFilename));
        }
        return $this->getImageMapping($filename);
    }
public function uploadFile ( $ filename ) { $ trimmedFilename = ltrim ( $ filename , '/' ) ; $ mediaDir = ltrim ( $ this -> getMediaDir ( ) , '/' ) ; $ imagesFileDir = ltrim ( $ this -> getImagesFileDir ( ) , '/' ) ; $ sourceFilename = sprintf ( '%s/%s' , $ imagesFileDir , $ trimmedFilename ) ; $ targetFilename = sprintf ( '%s/%s' , $ mediaDir , $ trimmedFilename ) ; if ( ! $ this -> getFilesystemAdapter ( ) -> isFile ( $ sourceFilename ) ) { throw new \ Exception ( sprintf ( 'Media file "%s" is not available' , $ sourceFilename ) ) ; } if ( $ this -> imageHasNotBeenMapped ( $ filename ) ) { $ newTargetFilename = $ this -> getNewFileName ( $ targetFilename ) ; $ targetFilename = str_replace ( basename ( $ targetFilename ) , $ newTargetFilename , $ targetFilename ) ; if ( ! $ this -> getFilesystemAdapter ( ) -> isDir ( $ targetDirectory = dirname ( $ targetFilename ) ) ) { $ this -> getFilesystemAdapter ( ) -> mkdir ( $ targetDirectory , 0755 ) ; } $ this -> getFilesystemAdapter ( ) -> copy ( $ sourceFilename , $ targetFilename ) ; $ this -> addImageMapping ( $ filename , str_replace ( $ mediaDir , '' , $ targetFilename ) ) ; } return $ this -> getImageMapping ( $ filename ) ; }
Upload's the file with the passed name to the Magento
 media directory.
Upload ' s the file with the passed name to the Magento media directory .
// trim the leading /, if available // prepare source/target filename // query whether or not the image file to be imported is available // query whether or not, the file has already been processed // load the new filename, e. g. if a file with the same name already exists // replace the old filename with the new one // make sure, the target directory exists // copy the image to the target directory // add the mapping and return the mapped filename // simply return the mapped filename