从php通过exec()调用php没有给出结果 [英] Calling php from php through exec() gives no result

查看:341
本文介绍了从php通过exec()调用php没有给出结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,根据用户输入创建其他PHP文件。基本上,有一些文件包含可以由用户翻译的特定于语言的常量( define )。为了避免运行时错误,我想测试新写入的文件的解析错误(由于不寻常的字符序列)。我在这里阅读了几个职位(如 PHP包含解析错误的文件) )并尝试使用

  $ output = exec(php -l $ filename)的函数

以确定文件是否正确解析。这在我的本地机器上完美工作,但是在提供程序的机器上,调用 exec(php ...)的输出似乎总是为空。我试着调用 ls ,它给了我输出,导致我假设PHP被某种方式配置为不响应命令行调用等。

编辑:我忘了提及,我已经尝试过 shell_exec code>,它也没有结果。为了回应sganesh的回答:我也试过了,对不起,我忘了提。但是,无论测试的PHP文件是否有语法错误,输出(第二个参数)总是一个空数组,返回值总是127。

解决方案

我通过使用不同的方法解决了我的原始问题。这是我现在做的:


  1. 用内容写入一个临时文件<?php include测试>; echoOK;

  2. 使用此网址执行HTTP请求

  3. 检查结果是否等于确定。

也许这个可以在没有临时文件的情况下通过向文件发出HTTP请求来直接测试。但是,如果存在解析错误,并且抑制了错误,则在不提供解析错误的文件的情况下,输出将为空,并且不能从输出中辨别。这种方法是有风险的,因为文件实际上是执行而不是只是检查。在我的情况下,只有有限数量的用户首先可以访问此功能。



为什么 exec()方法无效,我还是不知道。 pinaki可能是正确的,建议提供PHP可执行文件的完整路径,但我找不到完整的路径。



感谢大家的回答,我赞扬了你们。但是,我不能接受您的任何答案,因为没有您的建议真正解决我的问题。


I have a PHP script that creates other PHP files based on user input. Basically, there are files containing language specific constants (define) that can be translated by the user. In order to avoid runtime errors, I want to test newly written files for parse errors (due to "unusual" character sequences). I have read several posts here on SO (like PHP include files with parse errors) and tried a function that uses

$output = exec("php -l $filename");

to determine whether a file parses correctly. This works perfectly on my local machine, but at on the provider's machine, the output of calls to exec("php ...") seems to be always empty. I tried a call to ls and it gives me output, leading me to the assumption that PHP is somehow configured to not react to command line invocations or so. Does anyone know a way around this?

EDIT: I forgot to mention, I had already tried shell_exec and it gives no result, either. In response to sganesh's answer: I had tried that too, sorry I forgot to mention. However, the output (second argument) will always be an empty array, and the return value will always be 127, no matter if the PHP file to test has syntax errors or not.

解决方案

I worked around my original problem by using a different method. Here is what I do now:

  1. Write a temporary file with contents <?php include "< File to test >"; echo "OK"; ?>
  2. Generate the correct URL for the temporary file
  3. Perform HTTP request with this URL
  4. Check if result equals "OK". If yes, the file to test parses without errors.
  5. Delete temporary file

Maybe this could be done without the temporary file by issuing an HTTP request to the file to test directly. However, if there is a parse error and errors are suppressed, the output will be empty and not discernible from the output in the case of a file that gives no parse errors. This method is risky because the file is actually executed instead of just checked. In my case, there is only a limited number of users who have access to this functionality in the first place. Still, I'm naturally not entirely happy with it.

Why the exec() approach did not work, I still do not know exactly. pinaki might be right by suggesting to provide the full path to the PHP executable, but I cannot find out the full path.

Thank you everyone for answering, I upvoted you all. However, I cannot accept any of your answers as none of your suggestions really solved my problem.

这篇关于从php通过exec()调用php没有给出结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆