function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
zeezackzeezack 

Attachment form in php

How does one attach files to saleforce using php... it does not appear to store files in the conventional upload to a directory sense....

 

is it storing them in the body field of the attachment table?

Park Walker (TAGL)Park Walker (TAGL)

Yes, you load the base64 encoded content of the file into the body field the attachment. If you poke around on the boards you'll find more details.

 

Park 

nrwingnrwing

I have tried to load an attachment file to the case object via an apex trigger.  I converted it to base64, and it will process the load, but the resulting attachment cannot be downloaded/viewed...... it just comes up as a file with a really long text string (the base64 text of course) and not the actual file.... in this case an excel file......

 

Is there anything I am missing on this?  Not sure where to go from here....

Muhammad Nadeem AslamMuhammad Nadeem Aslam
Hello, If you want to use Force Rest API, you dont need to convert file content to base64. here is my working function for attachments to a Case object 
Just change the instance_url and access_token 
public function add_attachment($case_id, $full_file_path, $file_name) {

        $url = $this->instance_url."/services/data/v33.0/chatter/feed-elements";
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        
        

        $headers = array();
        $headers[] = "Authorization: OAuth $this->access_token";
        $headers[] = 'Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq';

    $post_text = '--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8

{
   "body":{
      "messageSegments":[
         {
            "type":"Text",
            "text":"Task Attachment"
         }
      ]
   },
   "capabilities":{
      "content":{
         "description":"Task Attachment",
         "title":"'.$file_name.'"
      }
   },
   "feedElementType":"FeedItem",
   "subjectId":"'.$case_id.'"
}

--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="feedElementFileUpload"; filename="'.$file_name.'"
Content-Type: image/png

'. file_get_contents($full_file_path).'

--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--';

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_text);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);

        $response_json = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        //print_r($info);
        
        if ( $status != 201 ) {
            $this->errors[] = "Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl);         
            return FALSE;
        }
        $this->status = $status;
        $this->curl_error = curl_error($curl);
        $this->curl_errno = curl_errno($curl);
        return json_decode($response_json,TRUE);
    }