title: Upload a file to slack with files.upload in PHP
body: <p>I'm trying to use the files.upload method to upload a file to Slack, but I only got a blank message so far.</p>
  
  <p>This is the code, I found this script on the internet, but it's not working.</p>
  
  <pre><code>&lt;?php
  include '../connessione.php';
  
  $slacktoken = "myToken"; //this is not the real token ofc
  $header = array();
  $header[] = 'Content-Type: multipart/form-data';
  $file = new CurlFile('../tmp/slack_icon.png', 'image/png');
  
  $postitems =  array(
      'token' =&gt; $slacktoken,
      'channels' =&gt; "test_channel",
      'file' =&gt;  $file,
      'text' =&gt; "This is my photo",
      'title' =&gt; "Photo title",
      'filename' =&gt; "myIcon.jpg"
  );
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_URL, "https://slack.com/api/files.upload");
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS,$postitems);
  
  //Execute curl and store in variable
  $data = curl_exec($curl);
  ?&gt;
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Thank you.</p>
  
  <p>Edit: after lots of tests I got this message if I echo the $data variable: </p>
  
  <pre><code>{"ok":false,"error":"missing_scope","needed":"files:write:user","provided":"identify,commands"}
  </code></pre>
  
  <p><strong>UPDATE</strong>
  I'll post here the full working code.</p>
  
  <pre><code>&lt;?php
  include '../connessione.php';
  
      // Buffer all upcoming output...
      ob_start();
  
      // Send your response.
      echo "Here be response";
  
      // Get the size of the output.
      $size = ob_get_length();
  
      // Disable compression (in case content length is compressed).
      header("Content-Encoding: none");
  
      // Set the content length of the response.
      header("Content-Length: {$size}");
  
      // Close the connection.
      header("Connection: close");
  
      // Flush all output.
      ob_end_flush();
      ob_flush();
      flush();
  
      // Close current session (if it exists).
      if(session_id()) session_write_close();
  
  
  $channel = $_POST['channel_id'];
  $slacktoken = "myToken";
  $header = array();
  $header[] = 'Content-Type: multipart/form-data';
  $file = new CurlFile('slack_icon.png', 'image/png');
  
  
  
  $postitems =  array(
          'token' =&gt; $slacktoken,
          'channels' =&gt; "test_channel",
          'file' =&gt;  $file,
          'text' =&gt; "This is my photo",
          'title' =&gt; "Photo title",
          'filename' =&gt; "myIcon.jpg"
      );
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_URL, "https://slack.com/api/files.upload");
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $postitems);
  
  //Execute curl and store in variable
  $data = curl_exec($curl);
  ?&gt;
  </code></pre>
  
  <p>The initial part is taken from this <a href="https://stackoverflow.com/a/28738208/10171297">continue processing php after sending http response</a>. That's needed to send an immediate answer back to Slack and avoid the "Timeout was reached" message.</p>