A form has been set up on the site, the data from which is sent to the telegram bot. The PHP script is like this
<?php
echo $TOKEN;
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
use Dotenv\Dotenv;
// Specify the path to the directory where the .env file is located
$dotenvPath = __DIR__;
// Create an instance of Dotenv and load environment variables
$dotenv = Dotenv::createImmutable(dirname(__DIR__));
$dotenv->load();
// Get the token value
$TOKEN = $_ENV['TOKEN'];
// Telegram bot token and channel ID
$TELEGRAM_CHAT_ID = "-1002017740876";
// Check for the presence of a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect data from the form
$product = $_POST['product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = $_FILES['attachments'];
$invoice = $_FILES['invoice'];
// Save data to the database or file (depends on your requirements)
// Send notification to Telegram
$message = "<b>Promindustry</b>\n";
$message .= "<b>Product Name: </b>".$product." \n";
$message .= "<b>Sender: </b>".$name." \n";
$message .= "<b>Phone: </b>".$phone." \n";
$message .= "<b>Email: </b>".$email;
file_get_contents("https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$TELEGRAM_CHAT_ID&parse_mode=html&text=".urlencode($message));
// Function to send a file to Telegram
function sendFileToTelegram($document, $caption = '') {
global $TOKEN, $TELEGRAM_CHAT_ID;
$url = "https://api.telegram.org/bot$TOKEN/sendDocument";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'chat_id' => $TELEGRAM_CHAT_ID,
'document' => new CURLFile($document['tmp_name'], $document['type'], $document['name']),
'caption' => $caption
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
}
// Send files to Telegram
if ($attachments['error'] == UPLOAD_ERR_OK) {
sendFileToTelegram($attachments, 'Attachment: Signboard, part photo, other information');
}
if ($invoice['error'] == UPLOAD_ERR_OK) {
sendFileToTelegram($invoice, 'Attachment: Details for invoicing');
}
// Send a response to the client (e.g., JSON response)
echo json_encode([ "status" => "success", "message" => "Form submitted successfully!", ]);
} else {
// Send a response to the client about the invalid request method
echo json_encode(["status" => "error", "message" => "Invalid request method!"]);
}
?>
The problem is that everything works fine as long as the file is less than 50 kilobytes in size. As soon as the file is larger, the form is not sent. I will be grateful for your help in solving this problem.
I tried to redo the script
Could there be an error in the handler here?
$(document).ready(function() {
$("#tg").submit(function(e) {
e.preventDefault();
// Sending data to the backend using AJAX
$.ajax({
type: "POST",
url: "process_form.php",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(response) {
// Handling a successful response
console.log(response);
// Display a success message to the user
},
error: function(xhr, status, error) {
// Handling an error
console.error(xhr.responseText);
// Display an error message to the user
}
});
});
});
Most probable reason for such behaviour can be your php.ini configuration.
Please locate your php.ini file and edit this two lines according to your requirements.
Also verify file limitations from telegram api if there are any