开发者

Calling php from php through exec() gives no result

I have a PHP script that creat开发者_JAVA技巧es 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 had the same problem. The solution that worked for me was found in running-at-from-php-gives-no-output. I needed to add output redirection.

$output = exec("php -l $filename 2>&1");


You can try with exec second and third arguments. second argument will have the output of the command. third argument will have the return value.

And exec will return only last line of the command.

$filename = "a.php";
$output = exec("php -l $filename",$op,$ret_val);
print $output."\n";
print $ret_val."\n";
var_dump($op);


By executing shell_exec(), you can see the output as if you executed that file via command line. You can just see if there is an error right here.

<?php
if (strpos(shell_exec('php -l file.php'), 'Syntax Error')) {
    die('An error!');
}

There may also be a possibility that shell_exec() or exec() may be disable by your host.


Nice idea to check the file validity :-)!

Now, from the PHP manual for exec():

Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have components in the path to the executable.

Can you check if this is not the case for you?

Also, can you check by providing the full path of the PHP interpreter in the exec() instead of only php. Let me know how you fare.

Pinaki


the correct way is to add >2&1 as tested on a windows system using imagemagick!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜