How can a docx file be unzipped in PHP to access its individual components?

To unzip a docx file in PHP and access its individual components, you can use the ZipArchive class to extract the contents of the file. You can then navigate through the extracted files to access the XML files that contain the text and formatting of the document.

$docxFile = 'example.docx';
$zip = new ZipArchive;
if ($zip->open($docxFile) === TRUE) {
    $extractPath = 'extracted/';
    $zip->extractTo($extractPath);
    $zip->close();

    $content = file_get_contents($extractPath . 'word/document.xml');
    // Access the content of the document.xml file for further processing

    // Clean up extracted files
    array_map('unlink', glob($extractPath . '*'));
    rmdir($extractPath);
} else {
    echo 'Failed to open the docx file';
}