从另一个 php 执行 php 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2841093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:51:22  来源:igfitidea点击:

Execute php file from another php

php

提问by nabrugir

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 文件?

回答by Ignacio Vazquez-Abrams

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

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

For example, in a.phpput:

例如,输入a.php

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

In b.phpput:

输入b.php

<?php
print "two";
?>

Prints:

印刷:

eric@dev ~ $ php a.php
onetwothree

回答by Mark Baker

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 正在对操作系统进行脱壳,除非操作系统有某种特殊的方式来了解如何执行文件,否则它将默认将其视为 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 脚本顶部的标点符号

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

回答by Rob Kennedy

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 <?phpat 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 execdoesn'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 execprobably 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 phpand pass your script as an argument. Then you shouldn't need the shebang line.

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

exec('php name.php');

回答by Amar

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!!

希望这可以帮助!!

回答by user3326612

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

exec('wget http://<url to the php script>')为我工作。

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 文件,并将它们作为代码运行以在不影响调用页面的情况下完成工作