Skip to content

Instantly share code, notes, and snippets.

@supernovaplus
Last active June 24, 2021 17:39
Show Gist options
  • Save supernovaplus/4e1056aa928c273dc82a449681daee61 to your computer and use it in GitHub Desktop.
Save supernovaplus/4e1056aa928c273dc82a449681daee61 to your computer and use it in GitHub Desktop.
PHP - Send file to Discord via Webhook
<?php
//from https://stackoverflow.com/questions/65133851/uploading-file-to-discord-with-webhook-in-php
//file is being called by a POST request in my case
//print_r for debugging
//default max size discord accepts is 8MB
echo "<pre>";
$webhookurl = "https://discord.com/api/webhooks/xxxx/xxxxxxx";
print_r($_FILES);
$json_data = [
"content" => "file uploaded - " . $_FILES["file"]["name"],
"tts" => "false",
"file" => curl_file_create($_FILES["file"]["tmp_name"], $_FILES["file"]["type"], $_FILES["file"]["name"])
];
print_r($json_data);
$curl = curl_init( $webhookurl );
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
$returned_data = curl_exec( $curl );
curl_close( $curl );
echo "---response---\n";
print_r(json_decode($returned_fileName));
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment