Shoe
Shoe

Reputation: 76240

Does this PHP constant exists?

Is there a PHP constant that automatically deletes $_SERVER['DOCUMENT_ROOT'] from __FILE__?

So that if Document root is:

/Applications/XAMPP/xamppfiles/htdocs

And __FILE__ is:

/Applications/XAMPP/xamppfiles/htdocs/Project/application/controllers/index.php

It returns:

/Project/application/controllers/index.php

Upvotes: 0

Views: 280

Answers (4)

Your Common Sense
Your Common Sense

Reputation: 157863

There is no such a predefined variable.

But you can easily get it from these two, using as basic string manipulation functions, as strlen() and substr()

Upvotes: 0

miku
miku

Reputation: 188024

You probably want _SERVER['PHP_SELF'] or _SERVER['SCRIPT_NAME'].

  • PHP_SELF: The filename of the currently executing script, relative to the document root.

  • SCRIPT_NAME: Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

About the difference between the two:

However, I just noticed a post on the php.general newsgroup where somebody asked what the difference was between them. Semantically, there isn't any; they should contain the same information. However, historically and technically speaking, there is. SCRIPT_NAME is defined in the CGI 1.1 specification, and is thus a standard. However, not all web servers actually implement it, and thus it isn't necessarily portable. PHP_SELF, on the other hand, is implemented directly by PHP, and as long as you're programming in PHP, will always be present.

Via: http://mwop.net/blog/45-PHP_SELF-versus-SCRIPT_NAME

Upvotes: 4

Cyclonecode
Cyclonecode

Reputation: 30001

Perhaps you could use

$_SERVER['SCRIPT_NAME'];

I don't know if I understood the question correctly, you could also concat the root and file

$_SERVER['DOCUMENT_ROOT'].$_SERVER['SCRIPT_NAME'];

Upvotes: 2

ppetree
ppetree

Reputation: 816

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?> 

Upvotes: 0

Related Questions