如何在没有shell访问的情况下获取php解释器/二进制文件的完整路径

2023-08-20php开发问题
1

本文介绍了如何在没有shell访问的情况下获取php解释器/二进制文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何从 php 脚本中获取 php 解释器的完整路径(无命令行访问权限).

我需要做的是:

$foo = "/usr/bin/php";回声 $foo;

但我需要先获取路径,以便将其分配给 foo.

如果您有一个在 Windows 和 nix 上都可以运行的更好的解决方案,但如果没有,nix 会很好.

在你问之前,

  1. 问主人是不可能的
  2. 没有外壳访问

问题是使用它输出的任何东西都不起作用.例如 PHP_BINDIR 将输出/usr/bin 但使用/usr/bin/php 无济于事.完整代码为:

exec("php-cli $path_to_file >/dev/null 2>/dev/null &");

但即使使用/usr/bin/php-cli 也不起作用,即使它告诉我.我必须使用:

exec("/opt/php52/bin/php-cli $path_to_file >/dev/null 2>/dev/null &");

例如对于这个特定的主机.

解决方案

你可以用这个常量找到 PHP 二进制路径:

PHP_BINDIR

从 PHP 5.4 开始,您可以使用此常量获取当前实际运行的可执行文件的路径:

PHP_BINARY

http://php.net/manual/en/reserved.constants.php

How can I get the full path to php interpreter from a php script (no command line access).

What I need to do is:

$foo = "/usr/bin/php";
echo $foo;

But I need to get the path first so I can assign it to foo.

If you have a solution that works on both Windows and nix even better but if not, nix would be fine.

Before you ask,

  1. Asking the host is out of the question
  2. No shell access

The problem is that using whatever it outputs doesn't work. For example PHP_BINDIR will output /usr/bin but using /usr/bin/php won't help. The full code is:

exec("php-cli $path_to_file > /dev/null 2>/dev/null &"); 

But even using the /usr/bin/php-cli doesn’t work even though it tells me that. I have to use:

exec("/opt/php52/bin/php-cli $path_to_file > /dev/null 2>/dev/null &");

For this particular host for example.

解决方案

You can find the PHP binary path with this constant:

PHP_BINDIR

As of PHP 5.4, you can get the path to the executable actually running currently with this constant:

PHP_BINARY

http://php.net/manual/en/reserved.constants.php

这篇关于如何在没有shell访问的情况下获取php解释器/二进制文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17