1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| <? Php
$path = "" ) función fixpath ($ path = "")
(
/ / Comprobar Sanidad
$path == "" ) { return false ; } if ($ camino == "") (return false;)
/ / Convierte todos los "\" a "/", y borra los espacios en blanco al principio y al final de la cadena
trim ( preg_replace ( "/ \\ \\ /" , "/" , ( string ) $path ) ) ; $ Path = TRIM (preg_replace ( "/ \ \ \ \ \ \ /", "/", (string) $ ruta));
/ * Escapadas de la cadena original en partes: "root" y "dir".
* "Raíz" puede ser "C: /" (Windows), "/" (Linux) o "http://www.something.com/" (URL). Este será el inicio de la cadena de producción.
* "Dir" puede ser "Windows / System", "root / html / ejemplos /", "includes / clases / class.validator.php", etc
* /
"/^( \\ /| \w : \\ /|(http|ftp)s?: \\ / \\ /[^ \\ /] \\ /)?(.*)$/i" , $path , $matches , PREG_SET_ORDER ) ; preg_match_all ( "/ ^ (\ \ / | \ w: \ \ / | (http | ftp) s?: \ \ / \ \ / [^ \ \ /] \ \ /)?(.*)$/ i ", $ PATH, $ coincidencias, PREG_SET_ORDER);
$matches [ 0 ] [ 1 ] ; Path_root $ = $ coincidencias [0] [1];
$matches [ 0 ] [ 3 ] ; Path_dir $ = $ coincidencias [0] [3];
/ / Si "dir" parte tiene una o más barras al principio, borra todos.
preg_replace ( array ( "/^ \\ / /" ) , array ( "" ) , $path_dir ) ; $ Path_dir = preg_replace (array ( "/ ^ \ \ / /"), array ( ""), $ path_dir);
/ / Escapadas de "dir" parte en cada barra
explode ( "/" , $path_dir ) ; $ Path_parts explotar = ( "/", $ path_dir);
/ / Crea un nuevo array con el camino correcto. Cada elemento es un nuevo directorio (o archivo en el que termina, si existe) en secuencia.
$i = $j = 0 , $real_path_parts = array ( ) ; $i < count ( $path_parts ) ; $i ) for ($ i = $ j = 0, $ real_path_parts = array (); $ i <count ($ path_parts); $ i )
(
$path_parts [ $i ] == '.' ) if ($ path_parts [$ i] == '').
(
continuar;
)
$path_parts [ $i ] == '' ) && ( $i != ( count ( $path_parts ) - 1 ) ) ) else if (($ path_parts [$ i] =='') & & ($ i! = (count ($ path_parts) - 1)))
(
array ( ) ; $ Real_path_parts = array ();
0 ; $ J = 0;
continuar;
)
$path_parts [ $i ] == '..' ) else if ($ path_parts [$ i] =='..')
(
isset ( $real_path_parts [ $j - 1 ] ) && $real_path_parts [ $j - 1 ] != '..' ) || ( $path_root != "" ) ) if ((isset ($ real_path_parts [$ j - 1]) & & $ real_path_parts [$ j - 1]!'..') = | | ($ path_root! = ""))
(
$real_path_parts ) ; array_pop ($ real_path_parts);
$ J -;
continuar;
)
)
$real_path_parts , $path_parts [ $i ] ) ; array_push ($ real_path_parts, $ path_parts [$ i]);
$ J ;
)
. implode ( "/" , $real_path_parts ) ; return $ path_root. implosión ( "/", $ real_path_parts);
)
?> |
Deje una respuesta