PHP set path of a file

-1

I wanted to know how do I get a path through a DEFINE.

Example:

I want to put the path of my css but I want to make it dynamic without having to put "../ path / style.css".

I want to type BASEPATH. '/path/style.css'

Well I hope I have been clear. I'm a bit lay in php.

    
asked by anonymous 08.07.2018 / 17:23

2 answers

3

With $ GLOBALS it might solve your problem.

  

$ GLOBALS is a super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

It would look something like this:

$GLOBALS['BASEDIR'] = '/seus/assets';

And on your front:

<link rel='stylesheet' href='<?php echo $GLOBALS["BASEDIR"];?>/css/qualquer.css'/>

In all your assets, you use the same expression for the base dir:

<?php echo $GLOBALS["BASEDIR"];?>

Dai, if by chance your folder structure changes, you just need to change the GLOBALS basedir.

I hope I have helped.

    
08.07.2018 / 17:46
-1

You can organize your project better, so you do not have to use a superglobal.

For example, put the index.php folder in the public folder as well as the folders with your assets, so your html would look like this.

<link rel="stylesheet" type="text/css" href="/assets/css/styles.css">

Remembering that this is how your server will be configured when you place your project online, there will always be a public_html or www folder.

The other folders with your code (classes, functions..etc ..) always put a level before, as protection.

    
09.07.2018 / 04:35