getcwd和__DIR__有什么区别? [英] What is the difference betweed getcwd and __DIR__?

查看:88
本文介绍了getcwd和__DIR__有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DIR 是如中所述的魔术常数。 PHP文档。根据 PHP文档的规定,getcwd()只是当前的工作目录。 p>

我的用例是:

  //这是我的index.php文件
require_once __DIR__。 ‘/vendor/autoload.php’;
$ app = new Silex\Application();
$ app [’debug’] = true;
$ app-> get('/ {name}',function($ name)use($ app){
return $ app-> sendFile(__ DIR__。''/ web / source / index .php');
});

我不完全理解为什么我需要这两种机制,因为我应该能够使用相对路径。



但是代码失败了。

解决方案

__ DIR __ 是当前正在执行的文件的位置。



getcwd()是当前目录中正在执行的php文件。请记住,您在服务器上而不是客户端上,并且需要注意您正在从哪个目录工作。



这可以更改。



有关此概念的更多信息,请参见此处


DIR is a magic constant as stated in the PHP docs. getcwd() is just the current working directory according to the PHP docs.

My use case is:

// this is my index.php file
require_once __DIR__ . '/vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->get('/{name}', function($name) use($app) {
    return $app->sendFile(__DIR__ . '/web/source/index.php');
});

I don't fully understand why I need either of these mechanisms as I should just be able to use relative paths.

However the code fails with out it.

解决方案

__DIR__ is where the currently executing file is.

getcwd() is the current directory php file is executing from. Remember you are on the server and not the client and need to be mindful of what directory you are working from.

This can change.

See here for more on this concept.

这篇关于getcwd和__DIR__有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆