/**
* Download file
*
* @param Entities\File $file
*
* @return boolean
*/
public static function downloadFile(File $file)
{
$path = $file->getFilePath();
//Create the directory
$basepath = self::$telegram->getDownloadPath();
$loc_path = $basepath . '/' . $path;
$dirname = dirname($loc_path);
if (!is_dir($dirname)) {
if (!mkdir($dirname, 0755, true)) {
throw new TelegramException('Directory ' . $dirname . ' can\'t be created');
}
}
//Open file to write
$fp = fopen($loc_path, 'w+');
if ($fp === false) {
throw new TelegramException('File can\'t be created');
}
$ch = curl_init();
if ($ch === false) {
throw new TelegramException('Curl failed to initialize');
}
$curlConfig = [CURLOPT_URL => 'https://api.telegram.org/file/bot' . self::$telegram->getApiKey() . '/' . $path, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0, CURLOPT_BINARYTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_FILE => $fp];
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
if ($result === false) {
throw new TelegramException(curl_error($ch), curl_errno($ch));
}
//Close curl
curl_close($ch);
//Close local file
fclose($fp);
return filesize($loc_path) > 0;
}