src/TgCommands.php
protected function processMediaUri($fileUri, $maxsizebytes = 10485760)
    {
        //Setup the mediafile Array to contain all the file's info.
        $mediaFileInfo = array();

        if (filter_var($fileUri, FILTER_VALIDATE_URL) !== false) {
            //The URI provided was a URL. Lets check to see if it exists.
            $mediaFileInfo = $this->checkUrlExistsAndSize($fileUri, $mediaFileInfo);

            if ( ! $mediaFileInfo || $mediaFileInfo['filesize'] > $maxsizebytes) {
                //File too big. Or doesn't exist. Don't Download.
                return false;
            }

            //Have we already downloaded this file before? If so, we should just reuse it!
            $originalFilename = pathinfo($fileUri, PATHINFO_BASENAME);
            $fileOnDisk       = sys_get_temp_dir() . '/' . $originalFilename;

            if (file_exists($fileOnDisk) && (filesize($fileOnDisk) == $mediaFileInfo['filesize'])) {
                $mediaFileInfo['filepath'] = $fileOnDisk;

                //File is identical (most likely) no need to redownload it.
                return $mediaFileInfo;
            };

            //So either file doesn't exist on local drive or a file with a different filesize is already there.
            $mediaFileInfo = $this->determineFilename($originalFilename, $mediaFileInfo);

            //Save to a new temp file name
            $tempFileName = fopen($mediaFileInfo['filepath'], 'w');
            if ($tempFileName) {
                $this->downloadMediaFileFromURL($fileUri, $tempFileName);
                fclose($tempFileName);
            } else {
                unlink($mediaFileInfo['filepath']);

                return false;
            }

            //Success! We now have the file locally on our system to use.
            return $mediaFileInfo;

        } else {
            if (is_file($fileUri)) {
                //URI given was a local file name.
                $mediaFileInfo['filesize'] = filesize($fileUri);
                if ($mediaFileInfo['filesize'] > $maxsizebytes) {
                    //File too big
                    return false;
                }
                $mediaFileInfo['filepath']      = $fileUri;
                $mediaFileInfo['fileextension'] = pathinfo($fileUri, PATHINFO_EXTENSION);

//                $mediaFileInfo['filemimetype']  = get_mime($filepath);

                return $mediaFileInfo;
            }
        }

        //Couldn't tell what file was, local or URL.
        return false;
    }
protected function processMediaUri($fileUri, $maxsizebytes = 10485760)
    {
        //Setup the mediafile Array to contain all the file's info.
        $mediaFileInfo = array();

        if (filter_var($fileUri, FILTER_VALIDATE_URL) !== false) {
            //The URI provided was a URL. Lets check to see if it exists.
            $mediaFileInfo = $this->checkUrlExistsAndSize($fileUri, $mediaFileInfo);

            if ( ! $mediaFileInfo || $mediaFileInfo['filesize'] > $maxsizebytes) {
                //File too big. Or doesn't exist. Don't Download.
                return false;
            }

            //Have we already downloaded this file before? If so, we should just reuse it!
            $originalFilename = pathinfo($fileUri, PATHINFO_BASENAME);
            $fileOnDisk       = sys_get_temp_dir() . '/' . $originalFilename;

            if (file_exists($fileOnDisk) && (filesize($fileOnDisk) == $mediaFileInfo['filesize'])) {
                $mediaFileInfo['filepath'] = $fileOnDisk;

                //File is identical (most likely) no need to redownload it.
                return $mediaFileInfo;
            };

            //So either file doesn't exist on local drive or a file with a different filesize is already there.
            $mediaFileInfo = $this->determineFilename($originalFilename, $mediaFileInfo);

            //Save to a new temp file name
            $tempFileName = fopen($mediaFileInfo['filepath'], 'w');
            if ($tempFileName) {
                $this->downloadMediaFileFromURL($fileUri, $tempFileName);
                fclose($tempFileName);
            } else {
                unlink($mediaFileInfo['filepath']);

                return false;
            }

            //Success! We now have the file locally on our system to use.
            return $mediaFileInfo;

        } else {
            if (is_file($fileUri)) {
                //URI given was a local file name.
                $mediaFileInfo['filesize'] = filesize($fileUri);
                if ($mediaFileInfo['filesize'] > $maxsizebytes) {
                    //File too big
                    return false;
                }
                $mediaFileInfo['filepath']      = $fileUri;
                $mediaFileInfo['fileextension'] = pathinfo($fileUri, PATHINFO_EXTENSION);

//                $mediaFileInfo['filemimetype']  = get_mime($filepath);

                return $mediaFileInfo;
            }
        }

        //Couldn't tell what file was, local or URL.
        return false;
    }
Takes a URI (in the form of a URL or local file path) and determines if
the file exists and that it is not too big. If the file is remote (ie a URL)
it will download the media file to the system temp directory for use.

@param string $fileUri
@param int    $maxsizebytes
@return array|bool