Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Send Photos and Files with the Telegram Bot API in PHP

A practical PHP approach to sending local photos and files through Telegram’s Bot API, with method selection, multipart upload, limits, and file_id reuse.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To send a local file from PHP, make an HTTPS request to Telegram’s sendPhoto or sendDocument method and attach the file using multipart/form-data. Use sendPhoto for photos and sendDocument for general files. If Telegram already has the file, you can send its file_id instead of uploading the bytes again.

Which Telegram method should PHP call?

Use case Method and required media field Accepted input Current documented constraint
Send a photo sendPhoto; photo Multipart upload, Telegram file_id, or an HTTP URL Telegram can fetch Maximum 10 MB; width plus height at most 10,000; width-to-height ratio at most 20
Send a general file sendDocument; document Multipart upload, Telegram file_id, or an HTTP URL Telegram can fetch Bots can currently send files of any type up to 50 MB; Telegram says this limit may change

These method inputs and limits are from Telegram’s sendPhoto and sendDocument references. For new local files, Telegram’s Bot API requires an HTTPS request with the file bytes sent as multipart/form-data; JSON can carry ordinary parameters but cannot upload the bytes. See Making requests.

As an Amazon Associate I earn from qualifying purchases.

How do I upload a photo from PHP?

The example below uses PHP’s cURL extension to implement Telegram’s HTTP contract. It sends one local file as the photo field and supplies chat_id as a regular multipart field. Set the bot token and chat ID from your application’s configuration, and point $path to a readable local image.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
$token = getenv('TELEGRAM_BOT_TOKEN');
$chatId = getenv('TELEGRAM_CHAT_ID');
$path = __DIR__ . '/photo.jpg';

if (!$token || !$chatId || !is_readable($path)) {
    throw new RuntimeException('Check the bot token, chat ID, and photo path.');
}

$url = "https://api.telegram.org/bot{$token}/sendPhoto";
$post = [
    'chat_id' => $chatId,
    'photo' => new CURLFile($path),
    'caption' => 'A photo from PHP',
];

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
    $error = curl_error($ch);
    curl_close($ch);
    throw new RuntimeException("HTTP request failed: {$error}");
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

$result = json_decode($response, true);
if (!is_array($result) || !($result['ok'] ?? false)) {
    $description = $result['description'] ?? 'Telegram returned an unreadable response.';
    $errorCode = $result['error_code'] ?? $status;
    throw new RuntimeException("Telegram error {$errorCode}: {$description}");
}

$message = $result['result'];
echo 'Photo sent; message ID: ' . $message['message_id'];
?>

Replace sendPhoto with sendDocument and the multipart field photo with document to send a general file. The caption field is optional. Telegram supports captions and caption-formatting parameters; consult the method reference for the options you need.

Keep the bot token in an environment variable or another protected configuration store, not in public source code or logs. The method URL contains the token, so avoid logging the full URL. Telegram returns JSON with an ok result and, on an unsuccessful request, error details such as description and error_code. The API documentation cautions that error-code contents may change; use the returned description and status when diagnosing failures.

Can I send a file without uploading it again?

Yes. When a send method returns a message containing the uploaded media, retain its Telegram file_id and use that value as the photo or document input on subsequent sends. Telegram recommends this route for files already stored on its servers, and its Bots FAQ says file IDs can be treated as persistent. See Telegram’s file_id guidance.

You can also supply an HTTP URL for Telegram to fetch, where the relevant method accepts it. A URL is not the same as a local PHP filesystem path: Telegram must be able to retrieve the URL over the network. For a repeat send, a stored file_id avoids sending the file bytes again from your server.

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

Why does getFile have a different limit?

getFile is for downloading a file from Telegram; it is not the method for sending one. Telegram’s Bots FAQ currently documents downloads through getFile for files up to 20 MB, while the current general send limit is 50 MB. Photo sends have their separate 10 MB and dimension limits. These are distinct operations and ceilings, not contradictory limits. See How do I download files? and How do I upload a large file?.

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

What should I check when a send fails?

  • Method and field: Use sendPhoto with photo, or sendDocument with document.
  • Multipart encoding: Attach the local file as a multipart file field. Do not JSON-encode the file bytes.
  • Local file access: Confirm PHP can read the path and that it points to the intended file.
  • Limits: Check the applicable send ceiling; for photos, also check dimensions and aspect ratio.
  • Response: Decode the response JSON and inspect ok, description, and error_code; also check whether the HTTP request itself failed.

Telegram’s live API documentation can change, including limits. Verify the current Bot API reference when size restrictions or parameters are critical to your implementation.

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.

Leave a Reply

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

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.

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.