Martin AJ
Martin AJ

Reputation: 6707

How can I make an absolute path for an specific file?

Here is my code:

<?php
    echo __DIR__."/../css/styles-home.css";
?>

It prints:

C:\xampp\htdocs\myweb\ltk\application/../css/styles-home.css

While this is the expected result:

C:\xampp\htdocs\myweb\ltk/css/styles-home.css

How can I get that ^ ?

Upvotes: 0

Views: 73

Answers (1)

John Denver
John Denver

Reputation: 362

Because you are just echo-ing the file path, the "../" isn't going to take you one step up in your directories, it's just going to print the "../". To go up one step, use the following code inline with your echo statement:

For PHP 5.3+ use:

echo realpath(__DIR__ . '/..')."/css/styles-home.css";

Or in PHP < 5.3 use:

echo realpath(dirname(__FILE__) . '/..')."/css/styles-home.css";

Upvotes: 1

Related Questions