Craft\Imager_ImagePathsModel::_downloadFile PHP 메소드

_downloadFile() 개인적인 메소드

Downloads remote file. Uses cURL if available, then tries with file_get_contents() if allow_url_fopen.
private _downloadFile ( $destinationPath, $imageUrl )
$destinationPath
$imageUrl
    private function _downloadFile($destinationPath, $imageUrl)
    {
        // url encode filename to account for non-ascii characters in filenames.
        $imageUrl = preg_replace_callback('#://([^/]+)/([^?]+)#', function ($match) {
            return '://' . $match[1] . '/' . join('/', array_map('rawurlencode', explode('/', $match[2])));
        }, urldecode($imageUrl));
        if (function_exists('curl_init')) {
            $ch = curl_init($imageUrl);
            $fp = fopen($destinationPath, "wb");
            $defaultOptions = array(CURLOPT_FILE => $fp, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 30);
            // merge default options with config setting, config overrides default.
            $options = craft()->imager->getSetting('curlOptions') + $defaultOptions;
            curl_setopt_array($ch, $options);
            curl_exec($ch);
            $curlErrorNo = curl_errno($ch);
            $curlError = curl_error($ch);
            $httpStatus = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
            curl_close($ch);
            fclose($fp);
            if ($curlErrorNo !== 0) {
                unlink($destinationPath);
                throw new Exception(Craft::t('cURL error “{curlErrorNo}” encountered while attempting to download “{imageUrl}”. The error was: “{curlError}”', array('imageUrl' => $imageUrl, 'curlErrorNo' => $curlErrorNo, 'curlError' => $curlError)));
            }
            if ($httpStatus !== 200) {
                if (!($httpStatus == 404 && strrpos(mime_content_type($destinationPath), 'image') !== false)) {
                    // remote server returned a 404, but the contents was a valid image file
                    unlink($destinationPath);
                    throw new Exception(Craft::t('HTTP status “{httpStatus}” encountered while attempting to download “{imageUrl}”', array('imageUrl' => $imageUrl, 'httpStatus' => $httpStatus)));
                }
            }
        } elseif (ini_get('allow_url_fopen')) {
            if (!@file_put_contents($destinationPath, file_get_contents($imageUrl))) {
                unlink($destinationPath);
                $httpStatus = $http_response_header[0];
                throw new Exception(Craft::t('“{httpStatus}” encountered while attempting to download “{imageUrl}”', array('imageUrl' => $imageUrl, 'httpStatus' => $httpStatus)));
            }
        } else {
            throw new Exception(Craft::t('Looks like allow_url_fopen is off and cURL is not enabled. To download external files, one of these methods has to be enabled.'));
        }
    }