How to get a absolute file path php
I have a folder abc and xyz.
I am including the file a.php of abc in xyz folder using Ajax request by giving relative path which is like:
../a.php
The file a.php contains some actions which are done using Ajax request.
In xyz folder i want to perform same actions which are perform in abc folder, but when i try to perform those actions it is searching for files in xyz folder instead of abc, so the actions which i want to perform in xyz are not working.
Please help me how to do this.
Updated code:
$(function(){
$.ajax({
type:"POST",
url: "../xyz/a.php",
data: {
"Id": '<?php echo $_GET['Id'];?>'
},
success: function(data){
$("#divId").html(data);
}
});
});
In Ajax request i have changed url like this
url: location.protocol + "//" + location.host + "projectname/foldername/filename.php"
Then it worked perfectly
Firstly, if you want to include a file in xyz
from abc
, you need to do ../abc/a.php
, not ../a.php
(unless, of course, the file is on the same level as your both directories).
Secondly, the function to convert relative paths to absolute is called realpath
and you can find the documentation here.
If your directory structure was something like this:
abc/
xyz/
a.php
...then to get the absolute path of a.php
from a file located in one of your directories, you would do
$absolute = realpath('../a.php');
I recommend you do something like this in your config/index/bootstrap:
define('ROOT_DIRECTORY', dirname(__FILE__).'/');
This way when you need to load files, from other locations, you make all the paths relative to the ROOT_DIRECTORY. for example:
require_once(ROOT_DIRECTORY . 'abc/xyz.php');
This will make file inclusions a LOT simpler and will allow you to move the entire project directory to another location ( production server for example ) without breaking any 'path' logic.
Seeing your code update, I see you're really talking about the request from the javascript.. in that case just leave the full url to the file: url:'http://server.com/xyx/abc.php'
Cheers
For example, you are in http://your-site.com/folder1/folder2/file.php.
here are php variables:
==============================================================================
======I======
__File__; C:\wamp\www\folder1\folder2\file.php
$_SERVER['PHP_SELF']; /folder1/folder2/file.php
//$_SERVER['PHP_SELF'] is the same as $_SERVER["REQUEST_URI"];
======II======
getcwd(); C:\wamp\www\folder1\folder2\
dirname(); OUTPUTS NOTHING - EMPTY NOT ALLOWED
basename(); OUTPUTS NOTHING - EMPTY NOT ALLOWED
__dir__; C:\wamp\www\folder1\folder2
======III======
getcwd( XXX ); OUTPUTS NOTHING - PARAMETER NOT ALLOWED
getcwd( XXX ); OUTPUTS NOTHING - PARAMETER NOT ALLOWED
getcwd( XXX ); OUTPUTS NOTHING - PARAMETER NOT ALLOWED
dirname(__FILE__); C:\wamp\www\folder1\folder2
dirname($_SERVER['PHP_SELF']); /folder1/folder2
dirname(getcwd()); C:\wamp\www\folder1
dirname(dirname()); OUTPUTS NOTHING - EMPTY NOT ALLOWED
dirname(basename()); OUTPUTS NOTHING - EMPTY NOT ALLOWED
basename(__FILE__); file.php
basename($_SERVER['PHP_SELF']); file.php
basename(getcwd()); folder2
basename(dirname()); OUTPUTS NOTHING - EMPTY NOT ALLOWED
basename(basename()); OUTPUTS NOTHING - EMPTY NOT ALLOWED
======IV======
on dirname
dirname(dirname(__FILE__)); C:\wamp\www\folder1
dirname(dirname($_SERVER['PHP_SELF'])); /folder1
dirname(dirname(getcwd())); C:\wamp\www
basename(dirname(__FILE__)); folder2
basename(dirname($_SERVER['PHP_SELF'])); folder2
basename(dirname(getcwd())); folder1;
on basename
dirname(basename(__FILE__)); .
dirname(basename($_SERVER['PHP_SELF'])); .
dirname(basename(getcwd())); .
basename(basename(__FILE__)); file.php
basename(basename($_SERVER['PHP_SELF'])); file.php
basename(basename(getcwd())); folder2
==============================================================================
============EXAMPLES===========
Home url
<?php echo $_SERVER['HTTP_HOST'];?>
ONLY current FILE url (like: mysite.com/myfile.php)
<?php echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>
Current url Fully (like: mysite.com/myfile.php?action=blabla
<?php echo $_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];?>
To get RealPath to the file (even if it is included) (change /var/public_html to your desired root)
<?php
// First step: Get full path
$cur_file=str_replace('\\','/',__FILE__);
// Second step: Remove the root path
$cur_file=preg_replace('/(.*?)\/var\/public_html/','',$cur_file);
?>
for wordpress, there exist already pre-defined functions to get plugins or themes url.