How to update custom property of docx file using php

Solution:

I able to update custom.xml using below code:

    $zip = new \ZipArchive;

    // Open this Zip File
    if ($zip->open('helloWorld.docx') == true) {
        // Get custom xml content
        $xmlContent = $zip->getFromName('docProps/custom.xml');

        // Update docPros/custom.xml content
        $updatedXmlContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <Properties
            xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
            xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
            <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="Id">
                <vt:lpwstr>121</vt:lpwstr>
            </property>
            <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="Notes">
                <vt:lpwstr>Lorem ipsum</vt:lpwstr>
            </property>
            <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="User">
                <vt:lpwstr>12</vt:lpwstr>
            </property>
        </Properties>';

        //Replace the content with the new content created above.
        $zip->addFromString('docProps/custom.xml', $updatedXmlContent);
        $zip->close();
    }

Answer

Solution:

The file is just XML. Use SimpleXML to modify the file

https://www.php.net/manual/en/simplexml.examples-basic.php

Source