How to upload files through the API?

Hi all, after some struggling and trial & error, I thought I’d share a PHP sample that works for uploading files to Discourse! Here ya go. Also, I’ve been creating an updated library originally from communiteq/discourse-api-php: PHP API client for Discourse (github.com):

<?php
	
	$curl = curl_init();
	
	curl_setopt_array($curl, array(
		CURLOPT_URL => 'https://example.com/uploads.json',
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_POSTFIELDS => array(
			'type' => 'composer',
			'synchronous' => 'true',
			'file'=> new CURLFILE('/directory/filename.txt')),
		CURLOPT_HTTPHEADER => array(
			'Content-Type: multipart/form-data',
			'Accept: application/json',
			'Api-Key: <your api key>',
			'Api-Username: <your api user id>'
		),
	));
	
	$response = curl_exec($curl);
	$info = curl_getinfo($curl);
	curl_close($curl);
	print_r(json_decode($response,1));
	print_r($info);
?>

Sample Output:

Array
(
    [id] => 627
    [url] => https://example.com/uploads/default/original/1X/4661e3a873857e04440bcc3e4290e241eb6c08d7.txt
    [original_filename] => filename.txt
    [filesize] => 461
    [width] => 
    [height] => 
    [thumbnail_width] => 
    [thumbnail_height] => 
    [extension] => txt
    [short_url] => upload://a2D9hHsjwsdrVWkzlu5Go4c1VvV.txt
    [short_path] => /uploads/short-url/a2D9hHsjwsdrVWkzlu5Go4c1VvV.txt
    [retain_hours] => 
    [human_filesize] => 461 Bytes
    [dominant_color] => 
    [thumbnail] => 
)
Array
(
    [url] => https://example.com/uploads.json
    [content_type] => application/json; charset=utf-8
    [http_code] => 200
    [header_size] => 422
    [request_size] => 1244
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.029617
    [namelookup_time] => 0.00046
    [connect_time] => 0.003265
    [pretransfer_time] => 0.003592
    [size_upload] => 911
    [size_download] => 460
    [speed_download] => 15531
    [speed_upload] => 30759
    [download_content_length] => 460
    [upload_content_length] => 911
    [starttransfer_time] => 0.029604
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 192.168.192.40
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.192.18
    [local_port] => 61613
    [http_version] => 2
    [protocol] => 1
    [ssl_verifyresult] => 0
    [scheme] => http
    [appconnect_time_us] => 0
    [connect_time_us] => 3265
    [namelookup_time_us] => 460
    [pretransfer_time_us] => 3592
    [redirect_time_us] => 0
    [starttransfer_time_us] => 29604
    [total_time_us] => 29617
    [effective_method] => POST
    [capath] => 
    [cainfo] => 
)

Also definitely got a HUGE help with the examples from the Postman samples here: Creates an upload | Discourse | Postman API Network

Cheers