Relative directories don't work the way you think they should in PHP
(according to your JavaScript way of thinking)
-- Mainly because PHP
is server side and theoreticaly can access any file within the operating system (unless jailed etc) -- The closest you're going to find is something like $_SERVER['DOCUMENT_ROOT']
which will give you the relative path to the web root -- However if you are running PHP
client based -- There is no "relative root" -- In cli
, you simply use ./
(or nothing), ../
, /
to tell php "related to the same directory as the script" IE
./directory/yourfile.txt //This starts in the SAME directory as the php file
../directory/yourfile.txt // This goes up one directory, and then you can drill back down.
../../../directory/directory/directory/file.txt // Just like above, but up 3 directories
/directory.file.txt // Starts from the server root -- IE /var/www/etc/etc/etc
Other than using these methods you can write your own way of handling this with your own methods etc -- There are many schools of though on this as well. Some say using absolute paths is the way to go ... But others argue that for portability relative is the way. Really it depends on your program and what you're using it for .. Whether it's getting deployed etc .. If it's the latter, and it's web-based, $_SERVER['DOCUMENT_ROOT']
will suffice, but you should structure your program to make classes, controllers, modules etc etc easily accessible when coding. (hence write methods to handle directory locations)
UPDATE per your comment
You asked if it was possible to pass this to php, and the answer is - yes, and no.
Yes, if the page has already loaded and you want to send the info as part of an action (form submit / AJAX call)
Yes, if you already have loaded the page, and have stored a cookie with this information on a previous page / session
No if a user has never been to the page and first pageload has already happened, and no other actions are expected to happen such as AJAX or form submissions.
Based on what you're asking you should be able to simply:
var mypath = document.getElementsByTagName('INPUT')[0].files[0].webkitRelativePath
$.post("send_path_to_php.php", {path: mypath}, function(result){
alert(result); // result of php page response
});
This will send the var
mypath to a php
file called send_path_to_php.php
. The php
file will be able to extract $_POST['path]
Then you do whatever you want in said php
-- Whatever you echo
/return
will be in the callback of result