dirname(__FILE__) on localhost

我与影子孤独终老i 提交于 2019-11-30 22:05:58

It looks like you just need to have

define('PATH', $_SERVER['SERVER_NAME']);

If you want to be super technical, you can do something like this instead.

define('PATH', str_replace($_SERVER['DOCUMENT_ROOT'], $_SERVER['SERVER_NAME'] . '/', dirname(__FILE__)));

On a side note, and more importantly, you don't actually need them. This will work.

<link rel="stylesheet" href="/css/style.css" />

When a href begins with a directory separator, it is considered relative to the document root, not the current working directory.

__FILE__ is a filesystem path, not an URL path. I think you may be getting confused about which you need. To include php files or move things around, youll want to use the filesystem path. To create URLs to resources youll want to use the URL.

For filesystem stuff you can use what the dirname(__FILE__). So in your front controller or top level entry points if youre not using the front controller pattern you might have things like:

define('ROOT_PATH', dirname(__FILE__));
define('INC_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'includes');

As far as asstes go (css, images, js) i like to keep these in a single location at the DOCUMENT_ROOT so the path is always /css/path/to/file.css regardless of where you are in the file structure. This can be a problem if you develop in subfolders on your local machine or testing server, but its easily avoided by using Virtual Hosts so that every site has its own file structure completely separate form others.

$server = str_replace('\\','/',$_SERVER['SERVER_NAME']);
$server = (substr($server,-1)=='/'?substr($server,0,strlen($server)-1):$server);
!defined('PATH')?define('PATH', 'http://'.str_replace($_SERVER['DOCUMENT_ROOT'],$server , str_replace('\\','/',dirname(__FILE__)))):'';
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!