file_exists php function - relative and absolute path

Asked

Viewed 785 times

0

In PHP applications, when I use absolute path, everything works perfectly, but when I use relative path, it doesn’t work.

An example: in my application I have to use this:

file_exists("/var/www/html/meus_projetos/dgnetwork/public_html/model/persistence/dao/dao_config/{$name}.ini")

On my local server apache2, I have a virtual hosts where I set the attribute DocumentRoot /var/www/html/meus_projetos/dgnetwork/public_html

With this I imagined that I could use the absolute path from public_html ie:

file_exists("model/persistence/dao/dao_config/{$name}.ini")

But it didn’t work.

I wonder if someone could help me organize it better?

  • I would recommend that you create a fixed location configuration file, where you would place all paths to directories and subdirectories of the site/project including the root, and initialize these settings at once..

3 answers

1

The question was simpler than I imagined. When starting the application, it is just me giving a require in a file, with this class constant and from there it will already be in memory. Problem solved, thank you all.

0

When you use file_exists("model/persistence/dao/dao_config/{$name}.ini") it will fetch from the location where the file.

You can set a constant for the root folder

define("DOCUMENT_ROOT", "/var/www/html/meus_projetos/dgnetwork/public_html/");

and then use it that way:

file_exists(DOCUMENT_ROOT."model/persistence/dao/dao_config/{$name}.ini")
  • so I’ll have code replication, and if I define a class of constants - final class constants {} - I’ll have to add the absolute path, which would imply ambiguity.

0

It works without being the absolute path however you may have problems.

Example:

I have a folder called a1 and inside it I have the file verifica_permissao.php In the briefcase before the a1 you create a test file and include the verifica_permissao.php, suppose that in check_permission . php contains the code below:

<?php
   //Exemplo do verifica_permissao.php
   function verifica_permissao($arquivo) {
        return file_exists($arquivo);
   }

While trying to use the verifica_permissao in case you try to access a file in the folder a2 is at the same level as a1 it will return false even if the file exists because it would try to access the following address:

/var/www/a1/a2/file.txt

In other words, consider creating a variable identifying where the root of your project is and always look for concatenating. So there will be no worry, look at an example below.

config.php file

<?php

     DEFINE("ROOT_DIR", __DIR__);

Test file.php

<?php
    require 'config.php';
    $arquivo = file_exists(ROOT_DIR . 'model/persistence/dao/dao_config/{$name}.ini');

    if($arquivo) {
        echo "Existe";
    } else {
        echo "Não existe.";
    }

Answering your question: No, there is no need to put the absolute path! It picks up from where the function is called.

  • That was very helpful. But what if I want to define my constants all within a class, within my application ? A class - final class app_const {} ? ai would define a private contrutor E constants ex: const DOCUMENT_ROOT = 'path'

  • by your example, I realized that you dispose the files in the same directory. But what if you are using a more specific project pattern, where one or more classes in different directories need to require config.php. How the file call will look ?????

  • What do you mean? I don’t understand

  • if you have already set in apache, you can take the absolute path using the $_SERVER array example: $_SERVER['DOCUMENT_ROOT']

Browser other questions tagged

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