• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/import/import.php
  • Класс: BitrixSaleLocationImportis
  • Вызов: is::downloadFile
static function downloadFile($fileName, $storeAs, $skip404 = false)
{
	$storeTo = $_SERVER['DOCUMENT_ROOT'].self::LOCAL_PATH;

	if(file_exists($storeTo))
	{
		if(!is_writable($storeTo))
			throw new MainSystemException('Temporal directory is not writable by the current user');
	}
	else
	{
		$dir = new IODirectory($_SERVER['DOCUMENT_ROOT']);
		$dir->createSubdirectory(self::LOCAL_PATH);
	}

	$storeTo .= $storeAs;

	if(file_exists($storeTo))
	{
		if(!is_writable($storeTo))
			throw new MainSystemException('Cannot remove previous '.$storeAs.' file');

		unlink($storeTo);
	}

	$query = 'http://'.self::DISTRIBUTOR_HOST.':'.self::DISTRIBUTOR_PORT.self::REMOTE_PATH.$fileName;
	$client = new HttpClient();

	if(!$client->download($query, $storeTo))
	{
		$eFormatted = array();
		foreach($client->getError() as $code => $desc)
			$eFormatted[] = trim($desc.' ('.$code.')');

		throw new MainSystemException('File download failed: '.implode(', ', $eFormatted).' ('.$query.')');
	}

	$status = intval($client->getStatus());

	if($status != 200 && file_exists($storeTo))
		unlink($storeTo);

	$okay = $status == 200 || ($status == 404 && $skip404);

	// honestly we should check for all 2xx codes, but for now this is enough
	if(!$okay)
		throw new MainSystemException('File download failed: http error '.$status.' ('.$query.')');

	// charset conversion here?

	return filesize($storeTo);
}