concrete/src/File/Importer.php
public function import($pointer, $filename = false, $fr = false, $prefix = null)
    {
        $fh = $this->app->make('helper/validation/file');
        $fi = $this->app->make('helper/file');
        $cf = $this->app->make('helper/concrete/file');

        $filename = (string) $filename;
        if ($filename === '') {
            // determine filename from $pointer
            $filename = basename($pointer);
        }

        $sanitizedFilename = $fi->sanitize($filename);

        // test if file is valid, else return FileImporter::E_FILE_INVALID
        if (!$fh->file($pointer)) {
            return self::E_FILE_INVALID;
        }

        if (!$fh->extension($filename)) {
            return self::E_FILE_INVALID_EXTENSION;
        }

        if ($fr instanceof FileEntity) {
            $fsl = $fr->getFileStorageLocationObject();
        } else {
            $fsl = $this->app->make(StorageLocationFactory::class)->fetchDefault();
        }
        if (!($fsl instanceof StorageLocation)) {
            return self::E_FILE_INVALID_STORAGE_LOCATION;
        }

        // store the file in the file storage location.
        $filesystem = $fsl->getFileSystemObject();
        if ($prefix) {
            $prefixIsAutoGenerated = false;
        } else {
            // note, if you pass in a prefix manually, make sure it conforms to standards
            // (e.g. it is 12 digits, numeric only)
            $prefix = $this->generatePrefix();
            $prefixIsAutoGenerated = true;
        }

        $src = @fopen($pointer, 'rb');
        if ($src === false) {
            return self::E_FILE_INVALID;
        }
        try {
            $filesystem->writeStream(
                $cf->prefix($prefix, $sanitizedFilename),
                $src,
                [
                    'visibility' => AdapterInterface::VISIBILITY_PUBLIC,
                    'mimetype' => $this->app->make('helper/mime')->mimeFromExtension($fi->getExtension($sanitizedFilename)),
                ]
            );
        } catch (Exception $e) {
            if (!$prefixIsAutoGenerated) {
                return self::E_FILE_UNABLE_TO_STORE_PREFIX_PROVIDED;
            } else {
                return self::E_FILE_UNABLE_TO_STORE;
            }
        } finally {
            @fclose($src);
        }

        if (!($fr instanceof FileEntity)) {
            // we have to create a new file object for this file version
            $fv = File::add($sanitizedFilename, $prefix, ['fvTitle' => $filename], $fsl, $fr);
        } else {
            // We get a new version to modify
            $fv = $fr->getVersionToModify(true);
            $fv->updateFile($sanitizedFilename, $prefix);
        }

        $fv->refreshAttributes(false);
        foreach ($this->importProcessors as $processor) {
            if ($processor->shouldProcess($fv)) {
                $processor->process($fv);
            }
        }
        if ($this->rescanThumbnailsOnImport) {
            $fv->refreshThumbnails(true);
        }
        $fv->releaseImagineImage();

        return $fv;
    }
public function import($pointer, $filename = false, $fr = false, $prefix = null)
    {
        $fh = $this->app->make('helper/validation/file');
        $fi = $this->app->make('helper/file');
        $cf = $this->app->make('helper/concrete/file');

        $filename = (string) $filename;
        if ($filename === '') {
            // determine filename from $pointer
            $filename = basename($pointer);
        }

        $sanitizedFilename = $fi->sanitize($filename);

        // test if file is valid, else return FileImporter::E_FILE_INVALID
        if (!$fh->file($pointer)) {
            return self::E_FILE_INVALID;
        }

        if (!$fh->extension($filename)) {
            return self::E_FILE_INVALID_EXTENSION;
        }

        if ($fr instanceof FileEntity) {
            $fsl = $fr->getFileStorageLocationObject();
        } else {
            $fsl = $this->app->make(StorageLocationFactory::class)->fetchDefault();
        }
        if (!($fsl instanceof StorageLocation)) {
            return self::E_FILE_INVALID_STORAGE_LOCATION;
        }

        // store the file in the file storage location.
        $filesystem = $fsl->getFileSystemObject();
        if ($prefix) {
            $prefixIsAutoGenerated = false;
        } else {
            // note, if you pass in a prefix manually, make sure it conforms to standards
            // (e.g. it is 12 digits, numeric only)
            $prefix = $this->generatePrefix();
            $prefixIsAutoGenerated = true;
        }

        $src = @fopen($pointer, 'rb');
        if ($src === false) {
            return self::E_FILE_INVALID;
        }
        try {
            $filesystem->writeStream(
                $cf->prefix($prefix, $sanitizedFilename),
                $src,
                [
                    'visibility' => AdapterInterface::VISIBILITY_PUBLIC,
                    'mimetype' => $this->app->make('helper/mime')->mimeFromExtension($fi->getExtension($sanitizedFilename)),
                ]
            );
        } catch (Exception $e) {
            if (!$prefixIsAutoGenerated) {
                return self::E_FILE_UNABLE_TO_STORE_PREFIX_PROVIDED;
            } else {
                return self::E_FILE_UNABLE_TO_STORE;
            }
        } finally {
            @fclose($src);
        }

        if (!($fr instanceof FileEntity)) {
            // we have to create a new file object for this file version
            $fv = File::add($sanitizedFilename, $prefix, ['fvTitle' => $filename], $fsl, $fr);
        } else {
            // We get a new version to modify
            $fv = $fr->getVersionToModify(true);
            $fv->updateFile($sanitizedFilename, $prefix);
        }

        $fv->refreshAttributes(false);
        foreach ($this->importProcessors as $processor) {
            if ($processor->shouldProcess($fv)) {
                $processor->process($fv);
            }
        }
        if ($this->rescanThumbnailsOnImport) {
            $fv->refreshThumbnails(true);
        }
        $fv->releaseImagineImage();

        return $fv;
    }
public function import ( $ pointer , $ filename = false , $ fr = false , $ prefix = null ) { $ fh = $ this -> app -> make ( 'helper/validation/file' ) ; $ fi = $ this -> app -> make ( 'helper/file' ) ; $ cf = $ this -> app -> make ( 'helper/concrete/file' ) ; $ filename = ( string ) $ filename ; if ( $ filename === '' ) { // determine filename from $pointer $ filename = basename ( $ pointer ) ; } $ sanitizedFilename = $ fi -> sanitize ( $ filename ) ; // test if file is valid, else return FileImporter::E_FILE_INVALID if ( ! $ fh -> file ( $ pointer ) ) { return self :: E_FILE_INVALID ; } if ( ! $ fh -> extension ( $ filename ) ) { return self :: E_FILE_INVALID_EXTENSION ; } if ( $ fr instanceof FileEntity ) { $ fsl = $ fr -> getFileStorageLocationObject ( ) ; } else { $ fsl = $ this -> app -> make ( StorageLocationFactory :: class ) -> fetchDefault ( ) ; } if ( ! ( $ fsl instanceof StorageLocation ) ) { return self :: E_FILE_INVALID_STORAGE_LOCATION ; } // store the file in the file storage location. $ filesystem = $ fsl -> getFileSystemObject ( ) ; if ( $ prefix ) { $ prefixIsAutoGenerated = false ; } else { // note, if you pass in a prefix manually, make sure it conforms to standards // (e.g. it is 12 digits, numeric only) $ prefix = $ this -> generatePrefix ( ) ; $ prefixIsAutoGenerated = true ; } $ src = @ fopen ( $ pointer , 'rb' ) ; if ( $ src === false ) { return self :: E_FILE_INVALID ; } try { $ filesystem -> writeStream ( $ cf -> prefix ( $ prefix , $ sanitizedFilename ) , $ src , [ 'visibility' => AdapterInterface :: VISIBILITY_PUBLIC , 'mimetype' => $ this -> app -> make ( 'helper/mime' ) -> mimeFromExtension ( $ fi -> getExtension ( $ sanitizedFilename ) ) , ] ) ; } catch ( Exception $ e ) { if ( ! $ prefixIsAutoGenerated ) { return self :: E_FILE_UNABLE_TO_STORE_PREFIX_PROVIDED ; } else { return self :: E_FILE_UNABLE_TO_STORE ; } } finally { @ fclose ( $ src ) ; } if ( ! ( $ fr instanceof FileEntity ) ) { // we have to create a new file object for this file version $ fv = File :: add ( $ sanitizedFilename , $ prefix , [ 'fvTitle' => $ filename ] , $ fsl , $ fr ) ; } else { // We get a new version to modify $ fv = $ fr -> getVersionToModify ( true ) ; $ fv -> updateFile ( $ sanitizedFilename , $ prefix ) ; } $ fv -> refreshAttributes ( false ) ; foreach ( $ this -> importProcessors as $ processor ) { if ( $ processor -> shouldProcess ( $ fv ) ) { $ processor -> process ( $ fv ) ; } } if ( $ this -> rescanThumbnailsOnImport ) { $ fv -> refreshThumbnails ( true ) ; } $ fv -> releaseImagineImage ( ) ; return $ fv ; }
Imports a local file into the system.

@param string $pointer The path to the file
@param string|bool $filename A custom name to give to the file. If not specified, we'll derive it from $pointer.
@param \Concrete\Core\Entity\File\File|\Concrete\Core\Tree\Node\Type\FileFolder|null|false $fr If it's a File entity we assign the newly imported FileVersion object to that File. If it's a FileFolder entiity we'll create a new File in that folder (otherwise the new File will be created in the root folder).
@param string|null $prefix the prefix to be used to store the file (if empty we'll generate a new prefix)

@return \Concrete\Core\Entity\File\Version|int the imported file version (or an error code in case of problems)
Imports a local file into the system .