开发者

path relative to document root in PHP

开发者 https://www.devze.com 2023-01-31 19:21 出处:网络
Hi im using XAMP开发者_如何转开发P to host a site locally. The DocumentRoot is as follows: DocumentRoot \"D:/xampp/htdocs\"

Hi im using XAMP开发者_如何转开发P to host a site locally.

The DocumentRoot is as follows: DocumentRoot "D:/xampp/htdocs" The siteDir is in the htdocs folder and has a php folder underneath is.

I am trying to use php fopen with a file relative to the site root (D:/xampp/htdocs/site) as follows: $log = fopen("/log.log", "a") however this is creating the file log.log in D:/log.log

Any ideas why this is creating in D:/ ( I would have expected / to be web root relative)

Many Thanks


The / indicates you're starting from filesystem root. If you want to do things relative to document root you can do (it's just another method to do this)

chdir($_SERVER['DOCUMENT_ROOT']);
$fp = fopen('log.log', 'w');

With this solution the "working directory" will remain the document root.


you have used an absolute path in your code, so PHP acts right.

write a simple code and take a look at $_SERVER variable:

<?php
echo '<xmp>';
print_r($_SERVER);
echo '</xmp>';
?>

you'll find appropriate key to use in your code. before the log.log

in my case that would be like this:

<?php

$sRoot = $_SERVER['DOCUMENT_ROOT'];

$fp = fopen($sRoot . '/log.log', 'w');

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号