0

Is it possible to guess the absolute path of a relative path that I have specified for loading a XML file? This is my coding:

$strXML = __DIR__ . "/../xml/file.xml";
$xmlFile = simplexml_load_file($strXML);

$strPath = fullPathFunction($xmlFile);  //Desired function
6
  • Why guess when you can use realpath()? Commented Sep 10, 2018 at 4:15
  • @Phil I'm already using realpath() function, but when I use it, I'm just geting /home/user/public_html but my expected result is /home/user/public_html/xml/file.xml Commented Sep 10, 2018 at 4:32
  • Try $strXML = __DIR__ . '/../xml/file.xml'; assuming the path is relative to your .php script Commented Sep 10, 2018 at 4:58
  • @Phil I think we are near. __DIR__ is geting the path to my .php file, so it gets /home/user/public_html/php but my .xml file is inside /home/user/public_html/xml directory Commented Sep 10, 2018 at 5:10
  • Yes, I realise that. That is why the path /../xml/file.xml is appended to __DIR__. If __DIR__ is /home/user/public_html/php, then /home/user/public_html/php/../xml/file.xml will resolve (via realpath()) to /home/user/public_html/xml/file.xml. If you're now using __DIR__, please update the code in your question Commented Sep 10, 2018 at 5:14

1 Answer 1

2

You can use realpath function:

$strXML = "../xml/file.xml";
$xmlFile = simplexml_load_file($strXML);

$strPath = realpath($strXML);

This example works for me

Sign up to request clarification or add additional context in comments.

16 Comments

I'm already using realpath() function, but when I use it, I'm just geting /home/user/public_html but my expected result is /home/user/public_html/xml/file.xml
That's not sense. realpath would return the folder that contains the file, in this case /home/user/public_html/xml, please, print the var_dump of $file are used as argument in realpath function.
update: added the filename to complete the expected result.
The result is string(15) "../xml/file.xml"
please, check the screen i was posted, it's working for me :S
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.