how to store a file in a note while receiving the file from curl response ?


I'm currently trying to build a plugin that at some point will have to download a file from an external server and store it in a note.

Do you know if it is possible to do so while keeping cloud compatibleness ?

  • Hi 

    Find below a working code:

    $url is the url where the file is located (to be downloaded)

    $filePath is the location of file the be saved inside CRM filesystem (upload/{note_id})

    Unfortunately this code doesn't work on SugarOndemand.

    public function download(string $url, string $filePath) {
    $fh_res = fopen($filePath, 'w+');

    if($fh_res === false) return ['status' => 'error', 'error' => 'Could not open : ' . $filePath];

    self::$curl = curl_init();
    curl_setopt(self::$curl, CURLOPT_URL, $url);
    curl_setopt(self::$curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt(self::$curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt(self::$curl, CURLOPT_ENCODING, '');
    curl_setopt(self::$curl, CURLOPT_MAXREDIRS, 10);
    curl_setopt(self::$curl, CURLOPT_TIMEOUT, 30);
    curl_setopt(self::$curl, CURLOPT_FILE, $fh_res);

    $data = curl_exec(self::$curl);
    $httpCode = curl_getinfo(self::$curl, CURLINFO_HTTP_CODE);
    $errorCode = curl_errno(self::$curl);

    if($httpCode == 200 && $errorCode == CURLE_OK) {
    $response = ['status' => 'success', 'code' => 200, 'data' => json_decode($data, true)];
    } else if($httpCode != 200) {
    $response = ['status' => 'error', 'code' => $httpCode, 'error' => json_decode($data, true)['error']];
    } else {
    $response = ['status' => 'error', 'code' => $errorCode, 'error' => curl_error(self::$curl)];


    return $response;


    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Thanks,

    Actually, since it seems not feasible for cloud, we will try to make the external server call Sugar Apis to push these files.