src/HasMedia/HasMediaTrait.php
public function addMediaFromUrl(string $url, ...$allowedMimeTypes)
    {
        if (! $stream = @fopen($url, 'r')) {
            throw UnreachableUrl::create($url);
        }

        $temporaryFile = tempnam(sys_get_temp_dir(), 'media-library');
        file_put_contents($temporaryFile, $stream);

        $this->guardAgainstInvalidMimeType($temporaryFile, $allowedMimeTypes);

        $filename = basename(parse_url($url, PHP_URL_PATH));
        $filename = str_replace('%20', ' ', $filename);

        if ($filename === '') {
            $filename = 'file';
        }

        $mediaExtension = explode('/', mime_content_type($temporaryFile));

        if (! str_contains($filename, '.')) {
            $filename = "{$filename}.{$mediaExtension[1]}";
        }

        return app(FileAdderFactory::class)
            ->create($this, $temporaryFile)
            ->usingName(pathinfo($filename, PATHINFO_FILENAME))
            ->usingFileName($filename);
    }
public function addMediaFromUrl(string $url, ...$allowedMimeTypes)
    {
        if (! $stream = @fopen($url, 'r')) {
            throw UnreachableUrl::create($url);
        }

        $temporaryFile = tempnam(sys_get_temp_dir(), 'media-library');
        file_put_contents($temporaryFile, $stream);

        $this->guardAgainstInvalidMimeType($temporaryFile, $allowedMimeTypes);

        $filename = basename(parse_url($url, PHP_URL_PATH));
        $filename = str_replace('%20', ' ', $filename);

        if ($filename === '') {
            $filename = 'file';
        }

        $mediaExtension = explode('/', mime_content_type($temporaryFile));

        if (! str_contains($filename, '.')) {
            $filename = "{$filename}.{$mediaExtension[1]}";
        }

        return app(FileAdderFactory::class)
            ->create($this, $temporaryFile)
            ->usingName(pathinfo($filename, PATHINFO_FILENAME))
            ->usingFileName($filename);
    }
public function addMediaFromUrl ( string $ url , ... $ allowedMimeTypes ) { if ( ! $ stream = @ fopen ( $ url , 'r' ) ) { throw UnreachableUrl :: create ( $ url ) ; } $ temporaryFile = tempnam ( sys_get_temp_dir ( ) , 'media-library' ) ; file_put_contents ( $ temporaryFile , $ stream ) ; $ this -> guardAgainstInvalidMimeType ( $ temporaryFile , $ allowedMimeTypes ) ; $ filename = basename ( parse_url ( $ url , PHP_URL_PATH ) ) ; $ filename = str_replace ( '%20' , ' ' , $ filename ) ; if ( $ filename === '' ) { $ filename = 'file' ; } $ mediaExtension = explode ( '/' , mime_content_type ( $ temporaryFile ) ) ; if ( ! str_contains ( $ filename , '.' ) ) { $ filename = "{$filename}.{$mediaExtension[1]}" ; } return app ( FileAdderFactory :: class ) -> create ( $ this , $ temporaryFile ) -> usingName ( pathinfo ( $ filename , PATHINFO_FILENAME ) ) -> usingFileName ( $ filename ) ; }
Add a remote file to the medialibrary.

@param string $url
@param string|array ...$allowedMimeTypes

@return \Spatie\MediaLibrary\FileAdder\FileAdder

@throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
Add a remote file to the medialibrary .