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.
Recommended Free Tools
<?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.
#1 Best Overall
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.
Rank #2
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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →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?.
What should I check when a send fails?
- Method and field: Use
sendPhotowithphoto, orsendDocumentwithdocument. - 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, anderror_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.
Quick Recap
Rank #4
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.




