# Getting value of DOCUMENT\_ROOT from PHP

**URL:** <https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504>\
**Category:** General\
**Created:** [9 August 2021 05:14 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504 "2021-08-09T05:14:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 August 2021 05:14 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/1 "2021-08-09T05:14:59Z")

</div>

I'm running PHP on nginx (on a RPi-4B) that is also running Node-RED.

Is there a way to get hold of... $\_SERVER['DOCUMENT\_ROOT'] and use it within Node-RED ??

e.g. like executing \<?php echo $\_SERVER['DOCUMENT\_ROOT']; ?\> in a PHP script.

I can use the http request node to execute a script and return the value.  
 ![Screen Shot 08-08-21 at 09.31 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e940e7690ef1e303b084d8d7062853f97db76902.png)  
 ![Screen Shot 08-08-21 at 09.31 PM 002](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65bff195909e673888a833724149f06fdccd120a.png)  
I was hoping there might be a way to use something simple **like** the 'exec' node or equivalent.

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [9 August 2021 07:29 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/2 "2021-08-09T07:29:58Z")

</div>

I think you could run your php script directly in a terminal through a exec node.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 August 2021 08:20 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/3 "2021-08-09T08:20:49Z")

</div>

I've tried that but must be doing something wrong as I get a syntax error.  
Any suggestions?  
 ![Screen Shot 08-09-21 at 09.16 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac1679764548723563ad5c72f9a58ca4139a106b.png)  
 ![Screen Shot 08-09-21 at 09.19 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a63c54b851b6aea1c52ed9bb4c1af152debb97ce.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 August 2021 08:28 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/4 "2021-08-09T08:28:11Z")

</div>

You cannot put raw php into the exec node - just as you wouldn't try that on the command line.

1. put that line into a file
2. set the exec node to run `php yourFile.php`

You might need to find the full path to the `php` command.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 August 2021 09:57 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/5 "2021-08-09T09:57:02Z")

</div>

Hi Nick,  
I've followed your instructions to create a file and call it from the 'exec' node.  
It seems to run, well it doesn't give any errors - just code:0, but it doesn't return a value.  
Any further suggestions??

 ![Screen Shot 08-09-21 at 10.51 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f3009839352aeb13a137840ef0db7573efbc841.png)  
 ![Screen Shot 08-09-21 at 10.51 AM 001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/443de4e398ca557716296ac2c7a131e392d9779c.png)  
 ![Screen Shot 08-09-21 at 10.51 AM 003](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6ebd7b86dee8d65d173977ea6d9694c4622ec5c.png)  
 ![Screen Shot 08-09-21 at 10.51 AM 002](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23e49e31c482a8054d41e17e18e376caab1e2554.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 August 2021 10:16 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/6 "2021-08-09T10:16:44Z")

</div>

Does it run in a terminal?

Edit: Also set the debug node to show full output

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [9 August 2021 10:17 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/7 "2021-08-09T10:17:44Z")

</div>

I think the main problem is that the php process running the command and the process executing scripts when you use a http request are not the same processes. Maybe it's "easier" to read the nginx configuration to determine the `document_root`.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 August 2021 10:19 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/8 "2021-08-09T10:19:29Z")

</div>

And how do I do that (as I'm very new to nginx) ??

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [9 August 2021 10:38 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/9 "2021-08-09T10:38:28Z")

</div>

I'm not an nginx expert either but maybe you could analyse the `sites-enabled` directory of your nginx server? I believe the `root` is defined in one of those files there. On my ubuntu test machine it's `/etc/nginx/sites-enabled`. You can use the command `whereis nginx` to get the directories you are looking for.

**Edit** : Or you could still use the `http request` approach but instead of returning the absolute path, you are writing it into a file on your server (which only you know, the file I mean) and then read the content of that file from Node-RED.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 August 2021 10:41 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/10 "2021-08-09T10:41:58Z")

</div>

Thanks for the swift response - I'll give it a try.

**Edit** : Your idea of reading in the nginx file and filtering out the line with keyword 'root' worked fine. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 October 2021 10:42 UTC](https://discourse.nodered.org/t/getting-value-of-document-root-from-php/49504/11 "2021-10-08T10:42:04Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
