PHP set path to a file

Asked

Viewed 78 times

-1

I wanted to know how I do to get a way 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 BASEPATH type . '/path/style.css'

Well I hope I’ve been clear. I’m a bit of a layman in php.

  • I don’t understand, if you want to set your own base path, you can do with, for example, $base = "http://exemplo.com/caminho"; require "$base/style.css";

  • @Guilhermecostamilam show! and this right there I wanted. And what I researched and I was doing with FILE and DIR.

2 answers

3


With $GLOBALS it might solve your problem.

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

Would 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 dir base:

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

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

I hope I’ve helped.

  • It helped a lot @Andrew Ribeiro. That’s exactly what I wanted. It worked 100% here.

  • @Pablo F r o z put Andrew’s answer as RIGHT. Only you have the power to do this.

-1

You can organize your project better, so you don’t need to use a superglobal.

For example put inside the public folder index.php and also the folders with your Assets, so in your html it 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 put your project online, there will always be a public_html or www folder.

Other folders with their code (classes, functions..etc..) always put a level first, as protection.

Browser other questions tagged

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