从命令行运行用于 Web 的 PHP 代码

发布于 2024-11-29 00:34:28 字数 713 浏览 2 评论 0原文

我有 PHP 代码,需要地址 $_SERVER['DOCUMENT_ROOT'].'/subdir 其他 php 文件/file.php';

首先——这是包含内容的最佳正确方法吗?显然我不想使用像 '../../subdir/file.php'; 这样的路径,因为移动文件会破坏它。

但另一个有趣的问题是,如果我通过命令行运行此文件,则不会创建 $_SERVER 。我可以通过 $_SERVER['DOCUMENT_ROOT'] = '.'; 来伪造它,但我很好奇这是否是最佳实践。好像不是。

编辑:显然有很多方法可以给这只猫换皮,尽管我认为最好的做法是定义一个负责包含目录的变量(或常量)。如:

define('INC_DIR', $_SERVER['DOCUMENT_ROOT'].'/../includes');

or

if (PHP_SAPI == 'cli') {
    $_includes = '../includes';
} else {
    $_includes = $_SERVER['DOCUMENT_ROOT'].'/../includes/');
}

并在整个代码中使用前面提到的变量或常量。

I have PHP code that requires other php files by address $_SERVER['DOCUMENT_ROOT'].'/subdir/file.php';

First of all -- is this the best proper way to include things? Obviously I don't want to use path like '../../subdir/file.php'; because moving the file would break it.

But another interesting issue is that if I run this file through command line then $_SERVER is not created. I can fake it via $_SERVER['DOCUMENT_ROOT'] = '.'; but I'm curious to if this is the best practice. Seems like not.

Edit: Obviously there are many ways to skin this cat, although I think the best practice is to define a variable (or a constant) responsible for the include directory. Such as:

define('INC_DIR', $_SERVER['DOCUMENT_ROOT'].'/../includes');

or

if (PHP_SAPI == 'cli') {
    $_includes = '../includes';
} else {
    $_includes = $_SERVER['DOCUMENT_ROOT'].'/../includes/');
}

And use the aforementioned variable or constant throughout the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-12-06 00:34:28

我更喜欢在我的架构中使用文件夹定义系统。像这样的东西:

define( 'DIR_ROOT',dirname(__FILE__) );

在命令行和网络模式下都有效。在您的应用程序入口点(大多数情况下为index.php)中使用它,然后从该文件向外加载框架的其余部分。对应用程序的所有入站调用都应通过 .htaccess 或其他方法路由,以便它们调用 index.php?foo=bar 等。

我也讨厌一直输入 DIRECTORY_SEPARATOR,所以我通常会做出第一个定义:

define( 'DS' , DIRECTORY_SEPARATOR );

这使您以后可以执行以下操作:像这样的东西:

require_once( DIR_ROOT.DS.'myfolder'.DS.'myfile.class.php' );

I prefer to use a folder definition system in my architectures. Something like this:

define( 'DIR_ROOT',dirname(__FILE__) );

That works both in command line and web mode. Use that in your application entry point (index.php in most cases) and then load the rest of your framework from that file outward. All inbound calls to your application should be routed via .htaccess or other method so that they call index.php?foo=bar etc.

I also hate typing DIRECTORY_SEPARATOR all the time so I usually make the first definition:

define( 'DS' , DIRECTORY_SEPARATOR );

This enables you later to do something like this:

require_once( DIR_ROOT.DS.'myfolder'.DS.'myfile.class.php' );
_蜘蛛 2024-12-06 00:34:28

或者,如果您不想或不需要修改 php 文件,而只需要正常执行页面,则可以使用 curl。大多数 Linux 和 Unix 系统都安装了它。

$ curl http://www.example.com/myscript.php &> /dev/null

&> /dev/null 部分将输出发送到系统中的黑洞中,因此您不必查看请求返回的 HTML。

Alternatively if you don't want or need to modify your php files and you just need a page to be executed normally you could use curl. Most Linux and Unix systems have it installed.

$ curl http://www.example.com/myscript.php &> /dev/null

The &> /dev/null part sends the output into a black hole in the system so you don't have to see the HTML which was returned by the request.

恋竹姑娘 2024-12-06 00:34:28
if(PHP_SAPI == 'cli')
{
    $_SERVER['DOCUMENT_ROOT'] = '/path/to/webroot';
}
if(PHP_SAPI == 'cli')
{
    $_SERVER['DOCUMENT_ROOT'] = '/path/to/webroot';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文