title: Uploading file to Discord with webhook in PHP
body: <p>Here's what I have</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  header(&quot;Access-Control-Allow-Origin: *&quot;);
  header(&quot;Access-Control-Allow-Headers: x-user-id&quot;);
  header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  
  if (!isset(getallheaders()[&quot;X-User-ID&quot;]) || !isset($_FILES[&quot;audio&quot;])) {
      return;
  }
  
  $webhookurl = &quot;discord url&quot;;
  //$webhookurl = &quot;https://8f13d44091009d558421159246966183.m.pipedream.net&quot;;
  
  $json_data = [
      &quot;content&quot; =&gt; &quot;&lt;@&quot;.getallheaders()[&quot;X-User-ID&quot;].&quot;&gt;&quot;,
      &quot;tts&quot; =&gt; false,
      &quot;file&quot; =&gt; curl_file_create($_FILES[&quot;audio&quot;][&quot;tmp_name&quot;], &quot;audio/mpeg&quot;, &quot;sound.mp3&quot;) // &quot;@&quot;.realpath($_FILES[&quot;audio&quot;][&quot;tmp_name&quot;])
  ];
  
  if (isset(getallheaders()[&quot;X-User-ID&quot;])) {
      $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: application/json'));
      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_encode($json_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ));
      
      curl_exec( $curl );
      curl_close( $curl );
  }
  </code></pre>
  <p>All I get is a message with the <code>&quot;content&quot;</code> field (a mention in this case), no file though. Embeds work fine too.</p>
  <p>Here's what I've found:</p>
  <ul>
  <li>From Discord docs: <code>file  file contents   the contents of the file being sent one of content, file, embeds</code> (pretty useless).</li>
  <li><a href="https://github.com/m7rlin/DiscordWebhook/blob/master/lib/DiscordWebhook-v7.0.php" rel="nofollow noreferrer">This guy here</a> has code for uploading a file. I have copied his cURL headers, no dice.</li>
  <li><a href="https://birdie0.github.io/discord-webhooks-guide/structure/file.html" rel="nofollow noreferrer">This page</a> says that you have to use <code>multipart/form-data</code>. The Discord docs say you only have to use it for embeds. Neither of which I found to be true. Embeds work fine with <code>application/json</code>, and I can't get a message <em>at all</em> with <code>multipart/form-data</code>.</li>
  </ul>
  <p>What else I've tried:</p>
  <ul>
  <li>Swapping out <code>curl_file_create()</code> for the older <code>@/path/to/file</code> (see commented out above)</li>
  <li>Different <code>Content-Type</code>, like I said.</li>
  <li><code>file_get_contents()</code></li>
  <li><code>move_uploaded_file()</code> instead of going directly from <code>$_FILES</code>.</li>
  </ul>
  <p>Any ideas on what I'm missing? I guess my main problem is I'm not sure <em>what</em> format Discord expects...</p>