Execute php file from another php

前端 未结 5 1279
南笙
南笙 2020-12-29 01:32

I want to call a PHP file that starts like

I call from the PHP like this:



        
相关标签:
5条回答
  • 2020-12-29 01:45

    exec('wget http://<url to the php script>') worked for me.

    It enable me to integrate two php files that were designed as web pages and run them as code to do work without affecting the calling page

    0 讨论(0)
  • 2020-12-29 01:59

    It's trying to run it as a shell script, which interprets your <?php token as bash, which is a syntax error. Just use include() or one of its friends:

    For example, in a.php put:

    <?php
    print "one";
    include 'b.php';
    print "three";
    ?>
    

    In b.php put:

    <?php
    print "two";
    ?>
    

    Prints:

    eric@dev ~ $ php a.php
    onetwothree
    
    0 讨论(0)
  • 2020-12-29 02:00

    This came across while working on a project on linux platform.

    exec('wget http://<url to the php script>)
    

    This runs as if you run the script from browser.

    Hope this helps!!

    0 讨论(0)
  • 2020-12-29 02:04

    exec is shelling to the operating system, and unless the OS has some special way of knowing how to execute a file, then it's going to default to treating it as a shell script or similar. In this case, it has no idea how to run your php file. If this script absolutely has to be executed from a shell, then either execute php passing the filename as a parameter, e.g

    exec ('/usr/local/bin/php -f /opt/lampp/htdocs/.../name.php)') ;
    

    or use the punct at the top of your php script

    #!/usr/local/bin/php
    <?php ... ?>
    
    0 讨论(0)
  • 2020-12-29 02:04

    Sounds like you're trying to execute the PHP code directly in your shell. Your shell doesn't speak PHP, so it interprets your PHP code as though it's in your shell's native language, as though you had literally run <?php at the command line.

    Shell scripts usually start with a "shebang" line that tells the shell what program to use to interpret the file. Begin your file like this:

    #!/usr/bin/env php
    <?php
    //Connection
    function connection () {
    

    Besides that, the string you're passing to exec doesn't make any sense. It starts with a slash all by itself, it uses too many periods in the path, and it has a stray right parenthesis.

    Copy the contents of the command string and paste them at your command line. If it doesn't run there, then exec probably won't be able to run it, either.

    Another option is to change the command you execute. Instead of running the script directly, run php and pass your script as an argument. Then you shouldn't need the shebang line.

    exec('php name.php');
    
    0 讨论(0)
提交回复
热议问题