How to set the path to access files on procedural system

Asked

Viewed 254 times

5

I’d like to know how to use the path in a procedural system, the correct way to define the/the paths, and to access the files, and what is most important to know about. Researching I found more examples in OOP, and I found nothing here on this subject.

I’m using ../, and sometimes ../../../../ Rsrs then I think it’s time to fix it.

I know I can use define, but I was left in doubt of where to include, and I did not quite understand the functioning of DIRECTORY_SEPARATOR, and the options. In this question in the OS, has several paths defined, not just the root, and I want to know, using the procedural system, how do I use this. Create a file with all these rules for each directory? How to access these directories later?

I will post an example of using the path to generate an error log (which was used in this answer) and if someone were willing to explain the details, I think it would be interesting to illustrate the functioning.

ini_set('error_log', BASE_DIR . DIRECTORY_SEPARATOR . 'logs' . DS . 'php' . DIRECTORY_SEPARATOR . 'PHP_errors-' . date('Ym') . '.log');
  • Which version of php are you using? There are some things that simplify

  • version 5.5.4 @rray

  • My suggestion is similar to that of Soen, define a file with only constants, the first of them the root(ROOT_PATH) of the project, if you need can create other, TEMPLATE_PATH, LIB_PATH etc..

  • 1
  • And then what do I call it in the project files? I make a include in each file that will use the path? Or can I put the include only in connection file for example? Does not roll an answer? : -) Thanks.

  • 1

    Each file you use will need include. Ai calls the constants straight. include 'LIB_PATH/funcoes/data.php';.

Show 1 more comment

1 answer

1


I usually create a file define.php, with directories, using previous settings, much like the OS example, always finishing the bar setting.

<?php
define('ROOT_DIR', filter_input(INPUT_SERVER, 'DOCUMENT_ROOT') . '/');
  define('TEMPLATE_DIR', ROOT_DIR . 'class/');
  define('CLASS_DIR', ROOT_DIR . 'class/');
  define('LIB_DIR', ROOT_DIR . 'library/');
  // insira outros diretórios de interesse

In the code you can use concatenating without using bar at the beginning.

<?php
include TEMPLATE_DIR . 'template.html';
include LIB_DIR . 'foo/foo.php';

In the example code a constant is used DS which is commonly used in place of DIRECTORY_SEPARATOR. Many projects define after checking if it does not exist, for being a widespread practice it is necessary to do this check to not generate errors.

<?php
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}

However it is not necessary to use these constants.

In Windows both the slash (/) and the backslash ( ) are used as a path-separating character. In other environments, only the slash (/).

Source: http://php.net/manual/en/function.basename.php

That way the code could be written.

<?php
ini_set('error_log', BASE_DIR . '/logs/php/PHP_errors-' . date('Ym') . '.log');

Browser other questions tagged

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