{"id":567,"date":"2006-07-12T16:53:44","date_gmt":"2006-07-12T20:53:44","guid":{"rendered":"http:\/\/www.uvm.edu\/~waw\/blog\/?p=567"},"modified":"2006-07-12T16:53:44","modified_gmt":"2006-07-12T20:53:44","slug":"script-questions","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/waw\/2006\/07\/12\/script-questions\/","title":{"rendered":"Script questions"},"content":{"rendered":"<p>Student asked about&nbsp; <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold;font-style: italic\">I need to find the &quot;virtual directory value relative from the root&quot; of<\/span><br style=\"font-weight: bold;font-style: italic\" \/><span style=\"font-weight: bold;font-style: italic\">my website.<\/span><\/div>\n<p>Long reply:<\/p>\n<p>&#8216;&quot;virtual directory value relative from the root&quot; of my website. &#8216; may have several interpretations, and may have different values and meanings depending upon the context and the server environment.<\/p>\n<p>For example, you could use the PHP Magic Constant<\/p>\n<p><span style=\"font-weight: bold\">&nbsp;&nbsp;&nbsp; __FILE__<\/span><\/p>\n<p>as in<\/p>\n<pre>&nbsp;&nbsp;&nbsp; $virtual_directory_value = dirname( __FILE__ );<\/pre>\n<p>Which, in the case say of this script<\/p>\n<p>&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/www.uvm.edu\/~waw\/archives\/dog.php\">http:\/\/www.uvm.edu\/~waw\/archives\/dog.php<\/a><\/p>\n<p>has the value of<\/p>\n<p>&nbsp;&nbsp;&nbsp; \/&#8230;\/uvm.edu\/fs\/rack1e\/u\/staff\/waw\/public_html\/archives<\/p>\n<p>Which isn&#8217;t so much the &quot;virtual directory value relative from the root&quot; as it is the absolute path of the directory containing the script. And, on www.uvm.edu, at least, a value that may change, since sometimes CIT Technical Resources groups shifts users from one physical disk (rack1e above) to another without warning &#8212; so beware if you ever hard-code this value in a script as opposed to obtaining it from the Magic Constant.<\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-style: italic;font-weight: bold\">In PHP, tried the command line<\/span><br style=\"font-style: italic;font-weight: bold\" \/><span style=\"font-style: italic;font-weight: bold\">$HTTP_SERVER_VARS[&#8216;DOCUMENT_ROOT&#8217;] but this is not my virtual root, but<\/span><br style=\"font-style: italic;font-weight: bold\" \/><span style=\"font-style: italic;font-weight: bold\">something else entirely (it is \/racka5\/http-data\/htdocs). <\/span>For example,<\/div>\n<p>We have the following other $HTTP_SERVER_VARS<\/p>\n<pre>&nbsp;&nbsp;&nbsp; [SCRIPT_FILENAME] =&gt; \/users\/w\/a\/waw\/public_html\/archives\/dog.php<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; [SCRIPT_URI] =&gt; http:\/\/www.uvm.edu\/~waw\/archives\/dog.php<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; [SCRIPT_URL] =&gt; \/~waw\/archives\/dog.php<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; [REQUEST_URI] =&gt; \/~waw\/archives\/dog.php<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; [SCRIPT_NAME] =&gt; \/~waw\/archives\/dog.php<\/pre>\n<p><br style=\"font-family: Courier New\" \/><span style=\"font-family: Courier New\">$HTTP_SERVER_VARS[SCRIPT_FILENAME] <\/span>is a preferred alternative to Magic Constant __FILE__, as it returns a fixed alias for the absolute path name of the script. In general, any users alias is formed using the convention<\/p>\n<p>&nbsp;&nbsp;&nbsp; \/users\/x\/y\/xyzzzzz<\/p>\n<p>where x is the first letter of the use&#8217;rs NetID, y is the second letter of the user&#8217;s NetID, and xyzzzzz is the user&#8217;s netid. But again, such conventions are only needed if and when you have to hard-code such a value in a script (usually when you are using some package like older versions of phpBB or wordpress or mediawiki that cat figure it out themselves).<\/p>\n<p>I expect SCRIPT_FILENAME is what you think you are looking for. Then<\/p>\n<pre>$spaw_dir = '\/spaw\/';<\/pre>\n<pre>$spaw_root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'].$spaw_dir;<\/pre>\n<p>Becomes<\/p>\n<pre>$spaw_dir = '\/spaw\/';<\/pre>\n<pre>$spaw_root = dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME ']).$spaw_dir;<\/pre>\n<p>But now let&#8217;s put that in the context of the uvmsnow site. If your spaw code lives here<\/p>\n<p>&nbsp;&nbsp;&nbsp; \/users\/u\/v\/uvmsnow\/public_html\/spaw<\/p>\n<p>and the code you want to have USE spaw is here<\/p>\n<p>&nbsp;&nbsp;&nbsp; \/users\/u\/v\/uvmsnow\/public_html\/admin<\/p>\n<p>Then the code in, say, admin\/demo.php that reads<\/p>\n<pre>&nbsp;&nbsp;&nbsp; $spaw_dir = '\/spaw\/';<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; $spaw_root = dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME ']).$spaw_dir;<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; include $spaw_root.'spaw_control.class.php';<\/pre>\n<p>tries to include &quot;\/users\/u\/v\/uvmsnow\/public_html\/admin\/spaw\/&quot;<\/p>\n<p>But that ain&#8217;t right &#8212; unless you put a copy of \/spaw\/ in admin. But then ,if you want to use spew from code in another folder, you have to do the same thing. Messy.<\/p>\n<p>So, you could do something funky like this<\/p>\n<pre>&nbsp;&nbsp;&nbsp; $spaw_dir = '\/spaw\/';<br \/>&nbsp;&nbsp;&nbsp; $spaw_root = $HTTP_SERVER_VARS['SCRIPT_FILENAME'];<br \/>&nbsp;&nbsp;&nbsp; $i=0;<br \/>&nbsp;&nbsp;&nbsp; while (!file_exists($spaw_root.$spaw_dir) &amp;&amp; $i &lt; 10) {<br \/>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $i++;<br \/>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $spaw_root =dirname($spaw_root);<br \/> &nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; $spaw_root.=$spaw_dir;<br \/>&nbsp;&nbsp;&nbsp; include $spaw_root.'spaw_control.class.php';<\/pre>\n<p>The $i helps to keep the loop in check in case of typos, missing folders, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Student asked about&nbsp; I need to find the &quot;virtual directory value relative from the root&quot; ofmy website. Long reply: &#8216;&quot;virtual directory value relative from the root&quot; of my website. &#8216; may have several interpretations, and may have different values and &hellip; <a href=\"https:\/\/blog.uvm.edu\/waw\/2006\/07\/12\/script-questions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6517,8025],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","hentry","category-projects","category-scriptingprogramming"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":0,"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/waw\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}