October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Upload Files and Reuse File IDs in the Telegram Bot API with PHP

Use PHP cURL and CURLFile to upload a local file to Telegram, then save the returned file_id for later sends from the same bot. Learn the limits and download flow.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Upload a local file by sending it to the appropriate Telegram Bot API method as multipart/form-data. In PHP, put a CURLFile in cURL’s CURLOPT_POSTFIELDS array. After a successful response, save the returned file_id and pass that ID to later sends from the same bot. Do not use file_unique_id for sending: Telegram says it cannot be used to resend or download a file.

Choose how Telegram should get the file

The Bot API supports three approaches: upload a local file, have Telegram fetch a URL where supported, or send a file already stored by Telegram using its file_id. The right choice depends on where the file is and whether the bot has handled it before.

As an Amazon Associate I earn from qualifying purchases.

Method Use it when Important constraint
CURLFile multipart upload The file is on the PHP server and should be uploaded now. Telegram currently documents a 50 MB bot sending limit. Use multipart form fields.
URL The resource is accessible to Telegram at a URL. Support depends on the method and media type. For sendDocument, URL sending currently supports PDF and ZIP files.
file_id The same bot has already sent or received the media and needs to send it again. The ID is specific to that bot, and Telegram does not allow changing the media type when resending by ID.

Telegram’s Bot API documentation describes the current methods and limits. The examples below use sendDocument; use the corresponding method and response field for other media types.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Upload a local file from PHP with cURL

PHP’s CURLFile class is intended for uploading a file with CURLOPT_POSTFIELDS. Give it the local path and, optionally, a MIME type and filename. cURL will build the multipart body and boundary; do not set a bare Content-Type: multipart/form-data header yourself, because that can omit the boundary cURL generates.

<?php
$ch = curl_init('https://api.telegram.org/bot' . $botToken . '/sendDocument');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => [
        'chat_id' => $chatId,
        'document' => new CURLFile($path, $mimeType, $filename),
    ],
]);

$response = curl_exec($ch);
$curlError = curl_error($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($response === false) {
    throw new RuntimeException('cURL request failed: ' . $curlError);
}

$data = json_decode($response, true);
if ($httpStatus < 200 || $httpStatus >= 300 || !is_array($data) || ($data['ok'] ?? false) !== true) {
    throw new RuntimeException('Telegram rejected the request: ' . $response);
}

$fileId = $data['result']['document']['file_id'];
// Persist $fileId for this bot and this media type.

Set $botToken, $chatId, $path, $mimeType, and $filename to values appropriate for your application. Keep the bot token secret: do not place it in public code, expose it in client-side applications, or include it in logs. Check both the cURL result and Telegram’s JSON ok field before accessing result.document.file_id. A successful sendDocument response contains a Message, whose document object carries the ID.

Save and reuse the returned file_id

Store the file_id returned for the bot that uploaded or received the file. For a later document send, use the saved string as the document form value instead of constructing another CURLFile:

CURLOPT_POSTFIELDS => [
    'chat_id' => $chatId,
    'document' => $savedFileId,
],

Telegram’s Bots FAQ says file IDs can be treated as persistent. However, each bot has its own IDs; an ID cannot be transferred to another bot. A file may also have more than one valid ID for a single bot, so an ID should not be treated as a global content hash. If your application operates several bots, associate each saved ID with the bot identity and media type that produced it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not substitute file_unique_id

Telegram describes file_unique_id as an identifier that should remain stable over time and across bots, but it cannot be used to send or download a file. Save file_id for the bot’s send operations. Also keep the media type consistent: Telegram does not let you resend a video ID as a photo, for example.

Download a file the bot has received

To download media, call getFile with its file_id, then use the returned file_path in Telegram’s file download URL. The standard Bot API currently limits bot downloads to 20 MB. Telegram guarantees that the download URL remains valid for at least one hour; if it expires, call getFile again for a fresh path and URL. The original filename or MIME type may not be preserved, so record those when your application first receives the file if it needs them later.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Know the current file limits and URL caveat

Telegram’s current Bot API documentation lists a maximum bot send size of 50 MB and a maximum bot download size of 20 MB. These operational limits can change, so check the live Bot API file and method documentation when implementing or troubleshooting. URL fetching is not a universal substitute for upload: for sendDocument, the current documentation limits URL sending to PDF and ZIP files.

Best Value
Vintage API Developer Application Programming Interface T-Shirt
  • API Developer Special Edition For An API Developer is perfect for developers who love Application programming interface Development.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.