简体   繁体   English

系统('php file.php'); 不起作用,为什么?

[英]system('php file.php'); doesn't work, why?

neither 也不

<?php system('php file.php'); ?>

nor 也不

<?php system('/usr/bin/php file.php'); ?>

worked. 工作。 Why? 为什么?

I tried with -q , with !#/usr/bin/php etc. 我尝试使用-q ,使用!#/usr/bin/php等。

You are supposed to call it with -f, but it should work without it as well: 您应该使用-f来调用它,但是没有它也应该可以工作:

<?php system('/usr/bin/php -f file.php'); ?>

What do you mean by "it doesn't work"? “不起作用”是什么意思?

Did you want the contents to be outputted as if they were from your script? 您是否要像从脚本一样输出内容? Use include or require . 使用includerequire

Did you want the contents in a variable? 您是否想要变量中的内容? Use the backtick operator . 使用反引号运算符

You can see what you get back from the command by using the backtick operator instead of system. 您可以使用反引号(tick)运算符而不是system来查看从命令中得到的结果。

Could be due to safe mode. 可能是由于安全模式。

 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. 

http://www.php.net/system . http://www.php.net/system

如果没有更多信息,可能是PHP可执行文件的路径,file.php的路径或文件权限问题。

What happens when you pass in a full path to 'file.php'? 当您传递完整路径到“ file.php”时会发生什么?

Also, try to redirect the output (but stdout and stderr) to a file so that you can see the error messages that are generated. 另外,尝试将输出(但stdout和stderr)重定向到文件,以便可以看到生成的错误消息。

Thanks all for answering!! 谢谢大家的回答!

For 'doesn't work' I meant: it does not return or print anything. 对于“无效”,我的意思是:它不返回或打印任何内容。

I have two files a.php && b.php (with all permissions) and safe_mode is off 我有两个文件a.php && b.php(具有所有权限),并且safe_mode已关闭

b.php b.php

<?php
$a = system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);
print_r($a);
echo '-'; # for separation
print_r($b); ?>

and a.php 和a.php

<?php echo 'hello world'; ?>

and when I run b.php from my browser (localhost/b.php) it prints: 当我从浏览器(localhost / b.php)运行b.php时,将输出:

string(0) "" -int(5) 字符串(0)“” -int(5)

that means $b variable is 5 but... 5 what? 这意味着$ b变量是5,但是... 5是什么?

What does it say when you turn on error reporting? 当您打开错误报告时,它表示什么?

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$a = system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);
print_r($a);
echo '-'; # for separation
print_r($b);

When I change 当我改变

system('/usr/bin/php -f /Applications/MAMP/htdocs/a.php',$b);

for 对于

('/bin/php -f /Applications/MAMP/htdocs/a.php',$b);

then it says 127 instead of 5, I guess these are error codes 然后说127而不是5,我猜这是错误代码

You obviously don't understand how system function works. 您显然不了解系统功能如何工作。 What you really need is probably popen . 您真正需要的可能是popen Start the process with popen and then read its output with fgets for example. 使用popen开始该过程,然后例如使用fgets读取其输出。

Alternatively, you can use the backtick operator as already suggested by others. 或者,您可以使用其他人已经建议的反引号运算符。

Try: 尝试:

$output = array();
$returnCode = 0;
exec('/usr/bin/php -f /Applications/MAMP/htdocs/a.php 2>&1', $output, $returnCode);
print_r($output);

The 2>&1 redirects stderr to stdout, so any error messages will be captured in $output 2>&1将stderr重定向到stdout,因此任何错误消息都将在$output捕获

Hang on... What are you actually trying to achieve here? 等一下...您实际上想在这里实现什么? Just run a.php and return the output to the browser? 只需运行a.php并将输出返回到浏览器? In that case, include it. 在这种情况下,请包括在内 From the looks of your posted content of a.php ( <?php echo 'hello world'; ?> ), that seems to be what you want. 从您发布的a.php内容的外观(<?php echo'hello world';?>)看来,这就是您想要的。

If there's more to the content of a.php, then please post back and explain what you really want to do. 如果a.php的内容更多,请回发并解释您真正想要做的事情。

Try: 尝试:

$fp = popen('/usr/bin/php -f file.php', 'r');

if(false === $fp)
{
   // something bad happened: error handle
}

$contents = '';

while(false === feof($fp))
{
  $contents .= fgets($fp);
}

fclose($fp);    
echo $contents;

That will allow you to capture the subshell's output and trap for errors. 这将使您能够捕获子Shell的输出并捕获错误。

}} For 'doesn't work' I meant: it does not return or print anything. }}表示“无效”:它不会返回或打印任何内容。

Try get a terminal/shell on your server and try your system command. 尝试在服务器上安装终端/外壳,然后尝试使用系统命令。 If your OS is unixy you can use something like which php to find the correct path to the php cli. 如果您的操作系统是unixy,则可以使用类似php的方法来找到php cli的正确路径。

You did install the php cli right? 您确实安装了php cli吗? It generally isn't installed by default. 通常,默认情况下未安装它。

Does your web server do a chroot or something? 您的Web服务器是否执行chroot或其他操作? Perhaps the php cli doesn't exist in the environment where you are trying to use system(). 可能您尝试使用system()的环境中不存在php cli。

Instead of trying to call a script try doing a simple system('php -v') . 与其尝试调用脚本,不如尝试做一个简单的system('php -v') Once you get that to correctly output the php version number then add the call to your script. 一旦获取到正确输出的php版本号,然后将调用添加到脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM