Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 633 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return current web path in PHP

#1
Currently developing a PHP framework and have ran into my first problem. I need to be able to drop the framework into any folder on a server, no matter how many folders deep, and need to find that directory to use as a base URL.

For example, it currently works if I put the framework in the root of the server (

[To see links please register here]

), but if I were to put it in

[To see links please register here]

it does not work.
Reply

#2
To get the current path of the file you must use:

$path=getcwd();

This will return you if in windows for example `C:\blah\blah\blah` with no file name.
Reply

#3
Theres a bunch of useful stuff available for things like this in the `$_SERVER` array. Do a `var_dump($_SERVER);` to see which element(s) of the array you need.
Reply

#4
Sounds like you are looking for the relative path.

$_SERVER['SCRIPT_FILENAME']

should do the trick. The php.net site has good documentation on what is available to that

[To see links please register here]


Also, if you are ever curious about what else is there, and what the values are

<?php print_r($_SERVER); ?>

will tell you more that you thought you could know.
Reply

#5
dirname(__FILE__);
basename(__FILE__);
print_r($_SERVER);
pathinfo('www/htdocs/index.html');
realpath('../../dir1/');
parse_url('http://username:password@hostname/path?arg=value#anchor');
Reply

#6
In order to make urls work check the [base tag](

[To see links please register here]

):

<base href="http://cms.dev/folder/" />

If the PHP file paths are the issue go with Pestilance's advice:

dirname(__FILE__) // returns the directory of current file
Reply

#7
`__FILE__` is a magic constant that returns the entire path of the current script. Combine with [dirname][1] and add ".." appropriately. It's more reliable than `getcwd`, since it cannot change during execution.

You can then strip off the web root, to get the relative path to your script (should map to URL). There are many `$_SERVER` variables that have this information. I suggest using the file system to determine:

1. If your script is publicly accessible?
2. At which depth / URL prefix?

Then combine with your base URL. If your script's path ==
`/home/public/foo_1/script.php`
... and your $_SERVER['DOCUMENT_ROOT'] ==
`/home/public`

Then you can rewrite your URL as `/foo_1/script.php`. You don't need the fully qualified URL, unless you want it. This technique works best if you execute it from a central location, like an autoloader.

[1]:

[To see links please register here]

Reply

#8
There are four existing answers, but they all seem to deal with file paths, and you're asking about a base URL for web requests.

Given any web request, you get a bunch of keys in `$_SERVER` that may be helpful. For example, in your mock example, you might have the following:

*

[To see links please register here]

— `$_SERVER['REQUEST_URI'] == /folder/`
*

[To see links please register here]

— `$_SERVER['REQUEST_URI'] == /folder/index.php`
*

[To see links please register here]

— `$_SERVER['REQUEST_URI'] == /folder/index.php/some/pathinfo`
*

[To see links please register here]

— `$_SERVER['REQUEST_URI'] == /folder/some/modrewrite`

Thinking critically, how would you pull out the base URL for any given subrequest? In certain cases you can look at `$_SERVER['REQUEST_URI']` and strip off trailing elements if you know how deep in your hierarchy the request is. (For example, if your script is two folders deep, strip off the last two path elements.) When `PATH_INFO` or `mod_rewrite` are in use, things become less clear: as longer and longer URLs are provided, there is no clear indication where the paths end and the dynamic URL begins.

This is why WordPress, MediaWiki, phpBB, phpMyAdmin, and every application I've ever written has the user manually specify a base URL as part of the application configuration.
Reply

#9
function GetCurrentWebDir() {
$CurrentPath = substr(
$_SERVER['SCRIPT_URL'], 0,
strlen($_SERVER['SCRIPT_URL']) - strlen(
strrchr($_SERVER['SCRIPT_URL'], "\\")
)
);

$CurrentFileName = basename(__FILE__);

return substr_replace(
$CurrentPath, '', -strlen($CurrentFileName),
strlen($CurrentFileName)
);
}

echo 'current dir:'.GetCurrentWebDir();`

cp:/apps/PHP/testtesttesttesttesttesttesttesttesttesttest.php
fn:testtesttesttesttesttesttesttesttesttesttest.php
len:48
current dir:/apps/PHP/

Reply

#10
Use [`dirname(__PATH__)`][1] to fetch the parent directory path.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through