TechBuzzPro

Upload Files to Telegram Bots in PHP & Reuse File ID

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to upload files to a Telegram bot using PHP and, more importantly, how to capture and reuse the file_id that Telegram returns. You'll learn to send documents, photos, and other media, then store the file_id so you can send the same file later without re-uploading it – saving bandwidth and time.

What You'll Need

  • A Telegram bot token (from @BotFather)
  • A web server with PHP 7.4+ and cURL enabled
  • Write access to a directory on your server (for temporary file storage)
  • Basic knowledge of PHP and HTTP requests

Step-by-Step

  1. Get your bot token – Open Telegram, message @BotFather, send /newbot, and follow the prompts. Copy the token (looks like 123456:ABC-DEF...).
  2. Create a PHP file – For example, telegram_upload.php. Start with a function to call the Telegram API:
function callTelegram($method, $params = []) {
    $url = "https://api.telegram.org/bot" . TOKEN . "/" . $method;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}
  1. Upload a file – Use sendDocument (or sendPhoto, sendAudio, etc.) with the @ prefix to tell cURL it's a file:
$chat_id = 'YOUR_CHAT_ID'; // e.g., 123456789
$file_path = '/path/to/your/file.pdf';
$params = [
    'chat_id' => $chat_id,
    'document' => new CURLFile($file_path),
    'caption' => 'Here is your file!'
];
$result = callTelegram('sendDocument', $params);
if ($result['ok']) {
    $file_id = $result['result']['document']['file_id'];
    echo "Uploaded! File ID: " . $file_id;
} else {
    echo "Error: " . $result['description'];
}
  1. Capture the file_id – In the response, the file ID is nested under result → document (or photo, audio, etc.). For photos, you'll get an array of sizes; the largest one's file_id is usually best for reuse.
  2. Store the file_id – Save it in a database, a JSON file, or even a text file. For example, append to a JSON array:
$store = ['file_id' => $file_id, 'caption' => 'My PDF'];
file_put_contents('files.json', json_encode($store) . PHP_EOL, FILE_APPEND);
  1. Reuse the file_id – To send the same file later, you don't need the actual file. Just pass the file_id as the document parameter:
$params = [
    'chat_id' => $chat_id,
    'document' => $file_id // just the string, no CURLFile
];
$result = callTelegram('sendDocument', $params);
  1. Handle different media types – For photos, use sendPhoto with photo parameter; for audio, sendAudio with audio; for video, sendVideo with video. The file_id works across all of them.
  2. Test thoroughly – Send a file to a test chat, capture the ID, then use that ID to send again. Verify the second send works without the original file.

Common Mistakes

  • Using @ prefix incorrectly – When reusing a file_id, do NOT use @ or CURLFile. Only use the raw string.
  • Wrong parameter name – For documents it's document, for photos it's photo, not file.
  • Not checking the response – Always check $result['ok']; if false, log $result['description'] to debug.
  • Using a local file path when reusing – If you pass a path again, Telegram will treat it as a new upload, defeating the purpose.
  • Storing the wrong file_id – For photos, you get multiple sizes; grab the one you need (usually the last element of the photo array).

Tips

  • Use a database – For production, store file_id along with a unique key (like a user ID or file name) in MySQL or SQLite.
  • Cache the file_id – If you send the same file repeatedly, fetch from your storage instead of uploading again.
  • Handle large files – Telegram's upload limit is 50 MB for bots; for larger files, consider using the getFile method to get a download link and send that.
  • Set a proper timeout – Uploads can take time; set CURLOPT_TIMEOUT to 60 seconds or more.
  • Use webhooks for receiving – If your bot receives files from users, use getFile to download them to your server.

Troubleshooting

  • "Bad Request: file is too big" – Telegram's limit is 50 MB for bots. Compress the file or use a different approach.
  • "Bad Request: wrong file identifier" – The file_id is invalid or expired. Re-upload the file to get a fresh ID.
  • cURL error: Could not resolve host – Check your internet connection and that api.telegram.org is reachable.
  • File uploads but bot doesn't respond – Verify your bot token and chat ID. Use getUpdates to see if the bot receives messages.
  • PHP warning: CURLFile not found – Ensure cURL extension is enabled in your php.ini.

Frequently Asked Questions

How long does a Telegram file_id remain valid?

File IDs are generally permanent for a given bot, but they can change if the bot is re-created. They are also bot-specific – you can't use a file_id from one bot in another.

Can I reuse a file_id for a different chat?

Yes, a file_id can be used to send the same file to any chat that your bot can access, as long as it's the same bot.

What if I need to send a file that was uploaded by a user?

When a user sends a file to your bot, you receive a file_id in the update. You can store it and reuse it later, just like a file you uploaded.

Is it possible to upload multiple files at once?

Telegram's API doesn't support sending multiple files in one message, but you can send multiple messages in a loop, each with its own file.

Do I need to store the file on my server to reuse it?

No, once you have the file_id, you don't need the actual file. That's the whole point – you can delete the local copy after the first upload.

Can I use a file_id to get the file back?

Yes, use the getFile method with the file_id to get a temporary download link (valid for 1 hour).