PHP: let a file return its own directory

好久不见. 提交于 2019-12-20 04:20:22

问题


I am having trouble finding the rigth directory on my online server. is there a function that returns the folder the file is stored in?


回答1:


In PHP>=5.3 use __DIR__ and before use dirname(__FILE__)

http://php.net/constants.predefined

http://php.net/dirname




回答2:


Use the magic constant __DIR__

You can read more about the magic constants here: http://php.net/manual/en/language.constants.predefined.php




回答3:


http://php.net/manual/en/function.getcwd.php

Returns the current working directory on success, or FALSE on failure.




回答4:


The dirname() function will return the directory portion of a path. For example, dirname('foo/bar/baz.php') will return foo/bar.

This is often used in conjunction with the __FILE__ magic constant for versions of PHP that do not support the __DIR__ magic constant (versions 5.2.x and below):

require_once dirname(__FILE__) . '/../classes/Class.php';


来源:https://stackoverflow.com/questions/5794362/php-let-a-file-return-its-own-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!