Using PHP to execute cmd commands

前端 未结 2 617
清歌不尽
清歌不尽 2020-12-09 21:50

How do I properly execute commands in the command line using php? For example I\'m using the command below in the command line to convert a docx file into a pdf file:

<
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 22:29

    Don't forget to escape your command with escapeshellcmd(). This will prevent you from having to use ugly backslashes and escape characters.

    There are also other alternatives which may work:

    `command` // back ticks drop you out of PHP mode into shell
    exec('command', $output); // exec will allow you to capture the return of a command as reference
    shell_exec('command'); // will return the output to a variable
    system(); //as seen above.
    

    Also, make sure your .exe is included within your $PATH variable. If not, include the full path for the command.

提交回复
热议问题