简体   繁体   中英

Why the path beginning with '/' points to 'C:/'?

I have created a site in localhost in the path C:/xampp/htdocs/seintian.altervista.org and then I hosted it in a hosting platform ( Altervista ).

After some months of programming in that directory I changed the root directory of the site in localhost to, simply, C:/xampp/htdocs because in that way I thought that I would have been able to run the site by searching http://localhost/ and, also, to use the paths /sub/dir/of/the/site instead of the relative ones like ../sub/dir/of/the/site , etc.

However, they somehow point to the path C:/ instead of C:/xampp/htdocs that is the DOCUMENT_ROOT of the Apache site (the local hosting is powered by XAMPP).

Plus, I tried to upload the server folder to Altervista, to see if there worked, but - and you can check by yourselves here - also there it didn't work, responding with a "file or directory not found at [...]" error.

Is it normal that /path/to/file points to C:/ even if the DOCUMENT_ROOT constant is set to C:/xampp/htdocs ? And, in any case, how can I make possible that paths like /sub/dir/of/the/site point to the DOCUMENT_ROOT , how expected?

PS: just to say, some paths in link elements in the head section of the page, point correctly to the right path, for example the assets of the page situated in C:/xampp/htdocs/assets . how is it possible? Does PHP just hate me? :'(

Thanks in advance <3

/... points to default storage, so in your case it's C: .

While links in webpage /... links to root directory of http server (do not mix directory path with URL path, they are not the same)

So these are pointing to same location, but has different meaning:

file_get_contents('/etc/dir/xampp/htdocs/assets/images/a.js');
<html>
   <head>
     <script src="/assets/images/a.js"></script>

I personally suggest to make your site portable by appending __DIR__ to all paths that are used in PHP:

// C:/xampp/htdocs/index.php
ROOT_DIR = __DIR__;

...

file_get_contents(ROOT_DIR  . '/assets/images/a.js');

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