简体   繁体   中英

Get root url of disk PHP

How can I get the root of the disk on my server? Like for example in windows I would like to get into the root of the drive C:/ but I want my code to be dynamic if we ever decide to use linux.

I have tried $_SERVER['DOCUMENT_ROOT'] but it only returns the root folder of the project.

Is there any way to do this in PHP?

You don't need to manually construct that / worry about it.

A better approach is to have a constant in your project that marks the path to it from the root.

To do this you can do something like

define('PROJECT_ROOT', __DIR__);

See more about __DIR__ here: https://www.php.net/manual/en/language.constants.predefined.php

Basically this takes care of the OS context for you so if your project is on windows it'll be C:\... and on Linux based systems just /...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM