Include PHP and directories?

Asked

Viewed 230 times

0

$url = "https://". $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'] ;

Result = localhost/Dashboard/folders/test/Florianopolis.php

This code above shows me everything, including the file name. It has some method of not showing the file?

So would you like it to stay = localhost/Dashboard/folders/test/

FILE show me this result = http://Applications/XAMPP/xamppfiles/htdocs/Dashboard/folders/test/footer.php

DIR show me this result = http://Applications/XAMPP/xamppfiles/htdocs/Dashboard/folders/test/

I don’t want the full path /Applications/XAMPP/xamppfiles/htdocs/, I only want localhost/Dashboard/folders/test/

Because this will serve both online and offline server.

Can someone please help me?

PS: I edited the previous question, so I think it’s clear!

  • What version of PHP?

  • could attach a pint of your project’s folder structure?

  • Why do not want to use the absolute path being that solves your problem?

  • 1

    PHP version is 5.3

  • I do not use the absolute path because this site has several domains and would not like to keep editing the absolute path for each domain.

  • Structure updated according to project.

Show 1 more comment

1 answer

2

To keep a relative path you should always go up to the root directory, and then look for the file to be included.

There are several ways to do this.

$_SERVER

$_SERVER['DOCUMENT_ROOT']

Magic constants

__DIR__

Access by include

./

etc....

Each one with its particularities, such as listing from the current document, and also destination variations by operating system

ai just concatenate with your destination and this path will always be used, idependentemente from the directory

  • One dumb question: this solution will serve both the server (Godaddy example) and easyphp?

  • If you agree with the php version, it will be fine

  • Felipe, How do I put this in the <script src="js/jquery.min.js"></script> that is in the footer? I’m not getting anywhere. Can you please help me?

  • Then try <script src="<? php echo $_SERVER['DOCUMENT_ROOT']. '/suaPastaPrincipal/'; ? >js/jquery.min.js"></script>

  • look in the browser console to see where it is pointing, so just tidy the folders until you find

  • Hi Felipe! First of all thank you very much for your help! Not to be boring but already being... Is it possible for php to get the exact url of the footer.php file? In the example you sent me, it works online without problems, but already on the server off, it doesn’t work because between root and the file there are directories (I use XAMPP). . $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'] shows me everything, including the file name. Have some method of not showing the file?

  • This is the echo of the above: https://localhost/Dashboard/folders/test/Florianopolis.php /

  • I didn’t understand it, but DIR shows the directory where your file is, abstaining from the file of course

  • "_ DIR ", if your php version is not compatible use dirname( FILE _), as the site here excludes the name of the constants follows the link http://php.net/manual/en/language.constants.predefined.php

  • Felipe, updated the post, I think it is now easier to understand. Again thank you.

  • Got it, try this

  • $url = explode('/', "http://". $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI']); unset($url[(Count($url) - 1)]); $url = implode('/', $url) . '/';

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.