Calling php from php through exec() gives no result

前端 未结 6 872
无人及你
无人及你 2021-01-14 06:24

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 tra

相关标签:
6条回答
  • 2021-01-14 06:54

    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.

    0 讨论(0)
  • 2021-01-14 07:01

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

    0 讨论(0)
  • 2021-01-14 07:02

    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

    0 讨论(0)
  • 2021-01-14 07:04

    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 讨论(0)
  • 2021-01-14 07:14

    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);
    
    0 讨论(0)
  • 2021-01-14 07:15

    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");
    
    0 讨论(0)
提交回复
热议问题