Object creation is not working for docx (word) file document using M-Files API endpoint

Hello,

I'm trying to build a simple program using PHP to create a new object within an attachment file included inside the object (docx) but always return corrupt / unreadable. But it is working well for PDF attachment file.

Code snippet :

                       

                       $base_url = 'http://localhost/REST';               
                       $obj_type_id = 0; //0 : document, 1 : other document
                       $headers = [
                             'X-Authentication: ' . $access_token,
                             'Content-Type: application/json',
                       ];
                        //Uploading file to M-Files temp folder
                        $content = file_get_contents($file_transfer_path);

                        curl_setopt($ch, CURLOPT_URL, $base_url . '/files.aspx');
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_POST, true);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $content]);

                        $uploadFileResponse = curl_exec($ch);
                     
                        $uploadInfo = json_decode($uploadFileResponse, true);

                        $uploadInfo['Extension'] = pathinfo($file_transfer_path, PATHINFO_EXTENSION);
                        $uploadInfo['Title'] = pathinfo($file_transfer_path, PATHINFO_FILENAME);

                        $file_data = [
                            'Files' => [[
                                "UploadID" => $uploadInfo['UploadID'],
                                "Title" => $uploadInfo['Title'],
                                "Extension" => $uploadInfo['Extension'],
                                "Size" => $uploadInfo['Size'],
                            ]],
                        ];

                        $post_data = array_merge($new_object_data, $file_data);

                        //Creating new object within content file
                        curl_setopt($ch, CURLOPT_URL, $base_url . "/objects" . "/" . $obj_type_id . '.aspx');
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

                        $createObjectResponse = curl_exec($ch);

                        echo "createObjectResponse : " . $createObjectResponse . "<br><br>";
Please kindly guide if you feel my code will fail.
Regards
Indri
Parents
  • I haven't used PHP in a few years, but I would recommend that you use something like a HTTP debugging proxy to see what HTTP requests are going back and forth, then compare that to the examples on the Developer Portal (and/or post them here).  My guess is that it's to do with how PHP is adding the file data to the "files" endpoint, or perhaps that some HTTP headers are missing/added.

Reply
  • I haven't used PHP in a few years, but I would recommend that you use something like a HTTP debugging proxy to see what HTTP requests are going back and forth, then compare that to the examples on the Developer Portal (and/or post them here).  My guess is that it's to do with how PHP is adding the file data to the "files" endpoint, or perhaps that some HTTP headers are missing/added.

Children
  • Hi Craig,

    Thanks for the response.

    I have tried with Postman, but the result is the same.

    Step 1 : Requesting the UploadID (http:localhost/REST/files)


    Step 2 : Creating the object (localhost/.../0)

    The JSON value attached is :

    {
                                "PropertyValues" : [
                                    {
                                        "PropertyDef" : 100,
                                        "TypedValue" : {
                                            "DataType" : 9,
                                            "Lookup" : {
                                                "Item" : 0,
                                                "Version" : -1
                                            }
                                        }
                                    },
                                    {
                                        "PropertyDef" : 0,
                                        "TypedValue" : {
                                            "DataType" : 1,
                                            "Value" : "ABCDEFGHIJKLMNO"
                                        }
                                    },
                                    {
                                        "PropertyDef" : 22,
                                        "TypedValue" : {
                                            "DataType" : 8,
                                            "Value" : true
                                        }
                                    }
                                ],
                                "Files" : [{
                                    "UploadID": 3,
                                    "Title" : "SPT LD 2020",
                                    "Extension" : "docx",
                                    "Size" : 13782
                                }]
    }
  • What's wrong here?  The docx file is then not usable?  Does the object look okay (correct extension, etc.) in M-Files?

    If so then I guess it's something to do with the first request; the one that uploads the file.  Take a look at the request headers; perhaps it's being transferred in a way that the server doesn't understand..

  • I have fixed this issue by changing file_get_content(), the PHP built in function (for some reason it is not working well for uploading the docx attachment but working for pdf attachment) to CURLFile() - that is working for all kinds of attachment's extension. Also, I set the header with no set of Content-Type.

                           $base_url = 'http://localhost/REST';               
                           $obj_type_id = 0//0 : document, 1 : other document
                           $headers = [
                                 'X-Authentication: ' . $access_token
                           ];
                            //Uploading file to M-Files temp folder
                            $content = ['file' => new CURLFile($file_transfer_path)];

                            curl_setopt($ch, CURLOPT_URL, $base_url . '/files.aspx');
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

                            $uploadFileResponse = curl_exec($ch);
                         
                            $uploadInfo = json_decode($uploadFileResponsetrue);

                            $uploadInfo['Extension'] = pathinfo($file_transfer_path, PATHINFO_EXTENSION);
                            $uploadInfo['Title'] = pathinfo($file_transfer_path, PATHINFO_FILENAME);

                            $file_data = [
                                'Files' => [[
                                    "UploadID" => $uploadInfo['UploadID'],
                                    "Title" => $uploadInfo['Title'],
                                    "Extension" => $uploadInfo['Extension'],
                                    "Size" => $uploadInfo['Size'],
                                ]],
                            ];

                            $post_data = array_merge($new_object_data$file_data);

                            //Creating new object within content file
                            curl_setopt($ch, CURLOPT_URL, $base_url . "/objects" . "/" . $obj_type_id . '.aspx');
                            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

                            $createObjectResponse = curl_exec($ch);

                            echo "createObjectResponse : " . $createObjectResponse . "<br><br>";