Add Media with API in PHP - Error Filetype Not Allowed


#1

Hello,

I want to upload a media with the api with php.
Upload a media with postman is actually working but when I try to do it in php, I always get a Filetype not allowed error :
[“files”]=>
array(1) {
[0]=>
object(stdClass)#5671 (4) {
[“name”]=>
string(15) “1536761420-4641”
[“size”]=>
int(0)
[“type”]=>
string(19) “multipart/form-data”
[“error”]=>
string(20) “Filetype not allowed”
}
}

My code, inpired by this implementation:

$file = file_get_contents($filepath);
//$file = fopen($filepath, ‘r’);
$body = array(
‘multipart’ => [
[
‘name’ => ‘name’,
‘contents’ => ‘API upload test’
],
[
‘name’ => ‘files’,
‘contents’ => $file
]
],
‘headers’ => array(“Authorization” => "Bearer ".$token, “Content-type” => “multipart/form-data”),
);
$response = wp_remote_post( get_option(‘xibo_cms_api_url’) . ‘/library’, $body );

In this code, I use wordpress customs functions but I’ve tried without in a alone php file using this as base and I had the same error.

Do you have a idea of what I’m doing wrong ?

Thanks,
Adrien


#2

We do have a file upload example using guzzle here - https://github.com/xibosignage/oauth2-xibo-cms/blob/master/example/file.php

Alternatively you could also use our ouath2 api library namely this wrapper - https://github.com/xibosignage/oauth2-xibo-cms/blob/master/src/Entity/XiboLibrary.php#L64
so then you could just call use this create wrapper and pass the media name and path to the file (and if replacing then oldMediaId, updateInLayouts, deleteOldRevisions)
which would make it a bit more straightforward (and you’d use one line of the code to do it.