get attached file name

Hi all, I am using the picture field in contacts to store an avatar image for my contacts. I want to be able to dowload this page via the api so I can store it on a website that the contacts use. i read the following page

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Web_Services/REST_API/PHP/… 

and the code 

//Get An Attachment on a Note
$url = $instance_url . "/Notes/{$noteRecord->id}/file/filename";

$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
//Return Headers
curl_setopt($curl_request, CURLOPT_HEADER, true);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
//DO NOT set Content Type Header to JSON
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "oauth-token: {$oauth_token}"
));

//execute request
$curl_response = curl_exec($curl_request);

//Get Return Headers
$header_size = curl_getinfo($curl_request,CURLINFO_HEADER_SIZE);
$headers = substr($curl_response, 0, $header_size);

//Outputting the file contents
echo 'File saved to download.txt';
$file = substr($curl_response, $header_size);
file_put_contents('download.txt', $file);

curl_close($curl_request);

but this code assumes the file name is download.txt, how do I get the filename first

in my example i can get the file info using {{localURL}}Contacts/0e05f7b4-9993-11e9-af70-06a63d65978a/file/ which returns

{
    "picture": {
        "content-type": "image/png",
        "content-length": 53843,
        "name": "ae2c25a0-4ad3-11ea-bbf1-080027633d6c",
        "width": 1611,
        "height": 1612,
        "uri": "rest/v11/Contacts/0e05f7b4-9993-11e9-af70-06a63d65978a/file/picture"
    }
}

but i dont get back the file name to then use in the curl request

 

 

 

Parents
  • Hi  john Fieldsend

     

    When a picture is uploaded to Sugar the name is changed to an ID and this file is placed inside the upload/ folder. 

    As far as I understand you are already getting the correct filename on your request.

     

    Can you check if you have a file in your upload/ folder named:

          ae2c25a0-4ad3-11ea-bbf1-080027633d6c

     

    This should match the "picture" field on your Contact record. 

     

    I hope this helps. 

     

  • Hi Andre Serrazina,

    I can see that file in my uploads folder I just wandered if Sugar saved the origonal file details like it does with an attachment field. I assume it doesn't for images so if i wanted to download it would need to get the extension from the content-type field

  • Hi john Fieldsend

    You are correct if you need to use this file in another system you will need to add the file extension that's returned in the content type, on this case it would be .png . 

    Let me know if this works for you. 

  • thanks Andre Serrazina,

    I will see if theres a class method to convert mime type to a file extension or write my own, thanks for clearing it up that image field type are treated differently to uploads. 

Reply Children
No Data