How do I get the files pulled from the root of the site?

Asked

Viewed 225 times

2

I’m creating a website... So far so good.

I created friendly Urls.

Before my URL was something like:

localhost/site/produto.php

And now it’s:

localhost/site/produtos/produto/planos

I wanted a code PHP pull all links from the site root.

Ex: Before my site pulled an image with the name imagem.png in the case imagens/imagem.png, more after changing my URL, now I have to use ../../imagens/imagem.png because if you don’t do this in the images for example, it will give all error 404.

Is there any way to make it change the directory url automatically without me having to edit one by one?

In case it would be:

    href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/imagens/imagem.png"

Now comes the problem: How to put the code above <?php echo $_SERVER['SERVER_NAME']; ?> automatically on all images via PHP?

  • Site root you refer to the initial Apache directory?

2 answers

6

A legal way is to have a constant defined having the root path of the site. For example:

define("SITE", "http://menusite.com.br/");

The pictures would look like this:

<img src="<?= SITE ?>imagens/minhaimagem.png" alt="" />

The nice thing about this approach is that it will work perfectly on localhost as well, as you can set the constant to the directory you want, for example, http://localhost/meusite/. As for editing one by one your references in code you can’t escape, but any IDE has a feature for running a general replace in the project.

6


You can set in two ways

1º - using "/" before links. Ex.:

<img src="/images/uploads/upload-10-50-100.png" alt="" />

2º - Create a domain with your scripts, images and css. Ex.:

<img src="http://assets.seusite.com.br/images/upload-10-50-100.png" alt="" />

There are more options and a tool for sure, but anyway, use the first option if you do not want to point to a domain.

Browser other questions tagged

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