# Upload Asset With API

**URL:** <https://discourse.getcockpit.com/t/upload-asset-with-api/2344>\
**Category:** Uncategorized\
**Created:** [April 27, 2022, 10:11am UTC](https://discourse.getcockpit.com/t/upload-asset-with-api/2344 "2022-04-27T10:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![seank1968](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/seank1968/32/1002_2.png) [@seank1968](https://discourse.getcockpit.com/u/seank1968)\
**Post date:** [April 27, 2022, 10:11am UTC](https://discourse.getcockpit.com/t/upload-asset-with-api/2344/1 "2022-04-27T10:11:15Z")

</div>

I have been trying to create a form that will allow an image upload to the assets - used the PHP from here: [https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/6](https://discourse.getcockpit.com/t/file-asset-upload-trough-the-api/1208/6) but I cannot get it to work at all!

I have:

```auto

<form method="POST" id="formid" enctype="multipart/form-data">
<input type="file" name="myFile" id="myFile">
      <button class="upload">Upload</button><br>
<img class="preview-img">
    </form>

<?php

function uploadImage($api,$token,$file){

    $url = 'http://mydomain.com/data/api/cockpit/addAssets?token=xxxxxxxxx';
    $cfile = new CURLFile(realpath($file));
    $post = ['files[]' => $cfile];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $result = curl_exec ($ch);

    return $result;
};

?>

```

This retrurns a null result.

Can anyone point me in the right direction to het this working please!

Thanks

---

<div class="post-metadata">

**Author:** ![ronaldaug](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/ronaldaug/32/260_2.png) [@ronaldaug](https://discourse.getcockpit.com/u/ronaldaug)\
**Post date:** [June 16, 2022, 5:32pm UTC](https://discourse.getcockpit.com/t/upload-asset-with-api/2344/2 "2022-06-16T17:32:05Z")

</div>

I tested the codes and I can confirmed this works 100%.  
I think you might be confused with Ajax and PHP Curl. As I made two accordion toggles, one for AJAX implementation with JS. And another one for PHP Curl.

If you want a full version of uploading via PHP, please try this below code. Make sure both Cockpit API and your php server are `https`, and no CORS problem.

```php
<?php

function uploadImage($api,$token,$file){

    $path = '/api/cockpit/addAssets?token=';
    $url = $api.$path.$token;
    $cfile = new CURLFile(realpath($file));
    $post = ['files[]' => $cfile];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $result = curl_exec($ch);

    return json_encode($result);
};

$API = 'http://mydomain.com';
$TOKEN = 'xxxxxxxxxxxxxxxxxxxxxx';

if($_FILES['myFile']){
    $FILE = $_FILES['myFile']['tmp_name'];
    $data = uploadImage($API,$TOKEN,$FILE);
    echo $data;
}

?>

<form method="POST" id="formid" enctype="multipart/form-data">
<input type="file" name="myFile" id="myFile">
      <button class="upload">Upload</button>
</form>

```
