php file_exists function - relative and absolute path

0

In PHP applications, when I use absolute path, everything works perfectly, but when I use relative path, it does not 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 DocumentRoot /var/www/html/meus_projetos/dgnetwork/public_html attribute

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 did not work.

Can anyone help me organize this better?

    
asked by anonymous 31.10.2015 / 18:43

3 answers

1

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

    
31.10.2015 / 21:00
0

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

You can set a constant for the root folder

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

and then use it this way:

file_exists(DOCUMENT_ROOT."model/persistence/dao/dao_config/{$name}.ini")
    
31.10.2015 / 18:57
0

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

  

Example:

I have a folder called a1 and inside it I have the file check_permission.php there in the folder that is before a1 you create a file test and includes the check_permission.php , suppose that in the check_permission .php contains the code below:

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

When trying to use the check_permission, if you try to access a file in the a2 folder that is at the same level as a1 it will return false even though the file exists because would try to access the following address:

  

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

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

  

Config.php file

<?php

     DEFINE("ROOT_DIR", __DIR__);
  

Test.php file

<?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, you do not have to put the absolute path! It picks up where the function is called.

    
31.10.2015 / 19:43