Publi

Barra al final del directorio en PHP: 4 formas a elegir

Cuando queremos representar un directorio; podemos obtenerlo de varias formas: /home/usuario/documentos en *nix o C:\Document and Settings\Usuario\Mis Documentos en Windows. Aunque lo mismo podemos representarlo /home/usuario/documentos/ y C:\Document and Settings\Usuario\Mis Documentos\ (es lo mismo, pero con una barra al final).

El problema en PHP, viene a la hora de llamar archivos de ese directorio, hay ocasiones en las que no sabemos si la variable en donde tenemos almacenado el directorio tiene barra al final o no; entonces debemos hacer alguna función que incluya la barra correspondiente si hace falta.

Estos dos primeros script los he sacado de Jonas John Code Snippets:

1
2
3
4
5
6
7
8
9
10
11
12
function add_ending_slash($path){
 
    $slash_type = (strpos($path, '\')===0) ? 'win' : 'unix';  
    $last_char = substr($path, strlen($path)-1, 1);
 
    if ($last_char != '
/' and $last_char != '\') {
        // no slash:
        $path .= ($slash_type == '
win') ? '\' : '/';
    }
 
    return $path;
}
1
2
3
4
5
6
7
8
9
10
11
12
function normalize_path($path){
 
    // DIRECTORY_SEPARATOR is a system variable
    // which contains the right slash for the current
    // system (windows = \ or linux = /)
 
    $s = DIRECTORY_SEPARATOR;
    $path = preg_replace('/[\/\\\]/', $s, $path);
    $path = preg_replace('/'.$s.'$/', '', $path) . $s;
 
    return $path;
}

Para este último, tenemos que hacer antes:

1
define("DIRECTORY_SEPARATOR", (PHP_OS=='Windows') ? '\':'/';

Otras dos alternativas:

1
2
3
4
5
6
function ending_slash($path)
{
  $sep = (PHP_OS == 'Windows')? '\':'/';
  $path .= (substr($path,-1) == $sep)? '
':$sep;
  return $path;
}
1
2
3
4
5
6
7
function ending_slash2($path)
{
  $sep = (PHP_OS == 'Windows')? '\':'/';
  if ($path)
    $path .= ($path[strlen($path)-1] == $sep)? '
':$sep;
  return $path;
}

La última no es muy elegante, pero ahí queda. Las dos últimas son muy parecidas entre sí. Y es una función interesante para nuestra biblioteca de funciones particular.

También podría interesarte....

There are 7 comments left Ir a comentario

  1. Pingback: Bitacoras.com /

  2. Alice Bobby /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    You truly are exceptional since you have a remarkable ability for motivating others with only a few words. retro bowl

  3. Allahabad Escort Service /
    Usando Google Chrome Google Chrome 122.0.0.0 en Windows Windows NT

    Experience refined companionship with our Call girls in AllahabadOur professional companions offer discreet and personalized encounters tailored to your desires. Whether you seek excitement in the city or tranquillity in nature, our escorts ensure unforgettable experiences filled with passion, excitement, and enchantment.

  4. Lucknow Escort Service /
    Usando Google Chrome Google Chrome 122.0.0.0 en Windows Windows NT

    Indulge in exquisite companionship with Guragon Escorts. Our professional escorts provide discreet and personalized experiences tailored to your desires. Explore the vibrant city of Bangalore with our captivating companions by your side, ensuring unforgettable moments filled with excitement, passion, and allure.

  5. Whitefield Escort Service /
    Usando Google Chrome Google Chrome 122.0.0.0 en Windows Windows NT

    Experience refined companionship with our Electronic City Escorts. Our professional companions offer discreet and personalized encounters tailored to your desires. Whether you seek excitement in the city or tranquillity in nature, our escorts ensure unforgettable experiences filled with passion, excitement, and enchantment.

  6. Mg Road Escort Service /
    Usando Google Chrome Google Chrome 122.0.0.0 en Windows Windows NT

    Explore premium companionship with our Ulsoor Escorts Our professional companions offer discreet and personalized encounters tailored to your desires. Whether you seek excitement in the city or tranquillity in nature, our escorts ensure unforgettable experiences filled with passion and allure.

  7. 토토사이트 /
    Usando Google Chrome Google Chrome 123.0.0.0 en Windows Windows NT

    It’s a game. Five dollars is free. Try it It’s not an easy game ->-> 토토사이트.com

Leave a Reply