Search code examples
phpshellterminaldocx

How to execute docx2txt shell script with Php?


I'm using docx2txt to extract .docx contents. I can achieve this by running the shell script in terminal.

below is the Terminal shell script,

 niveus@niveus:/var/www/docx2txt$ ./docx2txt.sh test.docx

 Text extracted from <test.docx> is available in <test.txt>.

But I want to run this script with php as well.

I tried this,

<?php

 $docxFilePath = "test.docx";
 echo  $content = shell_exec('./docx2txt.sh '.escapeshellarg($docxFilePath));

?>

and it outputs Failed to extract text from !

(both docx2txt.sh and test.txt are in the same folder docx2txt)


Solution

  • The issue was the permission. After changing the permission of the docx2txt folder to chmod 777 the following php code worked.

    <?php
    
     $docxFilePath = "test.docx";
     echo  $content = shell_exec('./docx2txt.sh '.escapeshellarg($docxFilePath));
    
    ?>