php - 从 php 执行 linux 程序

标签 php linux shell centos

使用以下软件:http://silvercoders.com/en/products/doctotext/

我使用以下代码让它在我的 Windows 机器上工作:

<?php    

$file = 'C:\xampp\htdocs\SMF\sdsdssss.docx';

$content = exec( 'C:\xampp\htdocs\SMF\doctotext\doctotext.exe /c '. $file , $output );

var_dump( $output );

但是,当我在我的 Centos 机器上尝试使用该软件的 linux x86_64 版本时,我无法让它工作。 (是的,我的机器也是x86_64)

我试过:

<?php

$file = '/home/***/public_html/downloads/test_doc.docx';

$content = shell_exec( '/home/***/public_html/doctotext/doctotext.sh \c '. $file , $output );

但没有输出。那些运行centos的等效执行格式是什么。

请指教

最佳答案

首先shell_exec()不接受第二个参数。我认为您将函数与 exec() 混淆了这确实需要多个参数:

exec('/home/***/public_html/doctotext/doctotext.sh \c '. $file, $output, $return);

来自 shell_exec() 的文档:

This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

其次,我认为您首先应该检查 shell 脚本是否具有执行权限。您可以使用以下方法设置正确的权限:

chmod +x /home/***/public_html/doctotext/doctotext.sh

根据脚本的用途,您可能还需要以 root 权限运行它:

sudo php -f myscript.php

如果您通过浏览器运行脚本,您还应该确保网络服务器可以读取 shell 脚本的路径。

如果您启用错误报告并检查您在运行脚本时是否收到任何错误消息,那就太好了。

关于php - 从 php 执行 linux 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967583/

相关文章:

bash shell 脚本条件赋值

Java SWT - 缩放背景图像在编译后不起作用

linux - 如何在 ssh linux 中运行多个命令

php - 如何根据条件从数据库加载

c++ - 制作[2] : *** No rule to make target `/usr/lib/i386-linux-gnu/libmpfr.so' in CGAL

javascript - 使用 php 创建测验,将数组数据存储在数据库的字段中

linux - AWK with $0 使用 sed 将模式替换为新字符串

linux - 硬件时钟有时会在未来

php - 标签的奇怪 HTML 忽略

php - 测试站点和生产站点的软件版本有所不同