从另一个php执行php文件

时间:2022-02-12 16:02:55

I want to call a PHP file that starts like

我想调用一个类似的PHP文件

<?php
function connection () {
   //Statements
}

I call from the PHP like this:

我这样从PHP调用:

<?php
exec ('/opt/lampp/htdocs/stuff/name.php');
?>

I get:

我明白了:

line1-> cannot open ?: No such file
line 3 //Connection: not found
line 4 Syntax errror: "("

Why doesn't this correctly execute the name.php file?

为什么这不正确执行name.php文件?

5 个解决方案

#1


47  

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:

它试图将它作为shell脚本运行,它将<?php标记解释为bash,这是一个语法错误。只需使用include()或其中一个朋友:

For example, in a.php put:

例如,在a.php中:

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

In b.php put:

在b.php中放:

<?php
print "two";
?>

Prints:

打印:

eric@dev ~ $ php a.php
onetwothree

#2


10  

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正在对操作系统进行shelling,除非操作系统有一些特殊的方法知道如何执行文件,否则它将默认将其视为shell脚本或类似文件。在这种情况下,它不知道如何运行您的PHP文件。如果绝对必须从shell执行此脚本,则执行php将文件名作为参数传递,例如

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

or use the punct at the top of your php script

或者使用php脚本顶部的punct

#!/usr/local/bin/php
<?php ... ?>

#3


7  

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中执行PHP代码。你的shell不会说PHP,所以它解释你的PHP代码就好像它是用你的shell的本地语言一样,好像你在命令行中运行<?php一样。

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:

Shell脚本通常以“shebang”行开头,该行告诉shell用于解释文件的程序。像这样开始你的文件:

#!/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.

除此之外,你传递给exec的字符串没有任何意义。它首先是一个斜杠,它在路径中使用了太多的句点,并且它有一个偏离右括号。

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.

复制命令字符串的内容并将其粘贴到命令行。如果它没有在那里运行,那么exec可能也无法运行它。

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.

另一种选择是更改您执行的命令。不是直接运行脚本,而是运行php并将脚本作为参数传递。然后你不应该需要shebang线。

exec('php name.php');

#4


2  

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

这是在linux平台上开展项目时遇到的。

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

This runs as if you run the script from browser.

这就像从浏览器运行脚本一样。

Hope this helps!!

希望这可以帮助!!

#5


-6  

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

exec('wget http:// ')为我工作。

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

它使我能够集成两个设计为网页的php文件,并将它们作为代码运行,而不会影响调用页面

#1


47  

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:

它试图将它作为shell脚本运行,它将<?php标记解释为bash,这是一个语法错误。只需使用include()或其中一个朋友:

For example, in a.php put:

例如,在a.php中:

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

In b.php put:

在b.php中放:

<?php
print "two";
?>

Prints:

打印:

eric@dev ~ $ php a.php
onetwothree

#2


10  

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正在对操作系统进行shelling,除非操作系统有一些特殊的方法知道如何执行文件,否则它将默认将其视为shell脚本或类似文件。在这种情况下,它不知道如何运行您的PHP文件。如果绝对必须从shell执行此脚本,则执行php将文件名作为参数传递,例如

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

or use the punct at the top of your php script

或者使用php脚本顶部的punct

#!/usr/local/bin/php
<?php ... ?>

#3


7  

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中执行PHP代码。你的shell不会说PHP,所以它解释你的PHP代码就好像它是用你的shell的本地语言一样,好像你在命令行中运行<?php一样。

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:

Shell脚本通常以“shebang”行开头,该行告诉shell用于解释文件的程序。像这样开始你的文件:

#!/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.

除此之外,你传递给exec的字符串没有任何意义。它首先是一个斜杠,它在路径中使用了太多的句点,并且它有一个偏离右括号。

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.

复制命令字符串的内容并将其粘贴到命令行。如果它没有在那里运行,那么exec可能也无法运行它。

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.

另一种选择是更改您执行的命令。不是直接运行脚本,而是运行php并将脚本作为参数传递。然后你不应该需要shebang线。

exec('php name.php');

#4


2  

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

这是在linux平台上开展项目时遇到的。

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

This runs as if you run the script from browser.

这就像从浏览器运行脚本一样。

Hope this helps!!

希望这可以帮助!!

#5


-6  

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

exec('wget http:// ')为我工作。

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

它使我能够集成两个设计为网页的php文件,并将它们作为代码运行,而不会影响调用页面