Eric @ EricBess WebHome

胜不骄,败不馁,胸有激雷而面如平湖

Chinese (Simplified) flagItalian flagKorean flagPortuguese flagEnglish flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroat flagDanish flagFinnish flagHindi flagPolish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flag
By N2H

PHP FixPath Function[路径整理函数]

FixPath Function: fix the dirty paths. PHP路径整理函数.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
echo "\n\n\n    Dirty paths:\n";
$paths[] = 'C://////////Windows//////System';               //  C:/System
$paths[] = 'C:\HTML\javascript\..\examples\colors.html';    //  C:/HTML/examples/colors.html
$paths[] = '/root/./wwwroot/scripts/../././webpage';        //  /root/wwwroot/webpage
$paths[] = 'wwwroot/webpage/../index.php?querystring';      //  wwwroot/index.php?querystring
$paths[] = 'http://www.php.net/manual/en/../../downloads';  //  http://www.php.net/downloads
$paths[] = 'http://www.php.net/downloads/test/test1/test2//./docs.php';    //  http://www.php.net/docs.php
$paths[] = '../downloads/../docs.php';                      //  ../docs.php
$paths[] = 'localhost//projetos/../_arquivos/../';          //   ""
$paths[] = 'C:/downloads/../../../';                        //  C:/
$paths[] = 'downloads/../../../';                           //  ../../
 
foreach ($paths as $path)
{
    echo "\n&quot;" . $path . "&quot;  =  &quot;" . fixpath($path) . "&quot;";
}
?>
Note: "//" one more slashes will roll back the root.


Function:

?Download fixpath.php
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
function fixpath($path = "")
{
    // Sanity check
    if ($path == "") {return false;}
 
    // Converts all "\" to "/", and erases blank spaces at the beginning and the ending of the string
    $path = trim(preg_replace("/\\\\/", "/", (string)$path));
 
    /*   Breaks the original string in to parts: "root" and "dir".
    *    "root" can be "C:/" (Windows), "/" (Linux) or "http://www.something.com/" (URLs). This will be the start of output string.
    *    "dir" can be "Windows/System", "root/html/examples/", "includes/classes/class.validator.php", etc.
    */
    preg_match_all("/^(\\/|\w:\\/|(http|ftp)s?:\\/\\/[^\\/]+\\/)?(.*)$/i", $path, $matches, PREG_SET_ORDER);
 
    $path_root = $matches[0][1];
    $path_dir  = $matches[0][3];
 
    //  If "dir" part has one or more slashes at the beginning, erases all.
    $path_dir = preg_replace(  array("/^\\/+/"),  array(""),  $path_dir  );
 
    // Breaks "dir" part on each slash
    $path_parts = explode("/", $path_dir);
 
    // Creates a new array with the right path. Each element is a new dir (or file in the ending, if exists) in sequence.
    for ($i = $j = 0, $real_path_parts = array(); $i < count($path_parts); $i++)
    {
        if ($path_parts[$i] == '.')
        {
            continue;
        }
        else if (($path_parts[$i] == '') && ($i != (count($path_parts) -1)))
        {
        	$real_path_parts = array();
        	$j = 0;
        	continue;
         }
        else if ($path_parts[$i] == '..')
        {
            if (  (isset($real_path_parts[$j-1])  &&  $real_path_parts[$j-1] != '..')  ||  ($path_root != "")  )
            {
                array_pop($real_path_parts);
                $j--;
                continue;
            }
        }
 
        array_push($real_path_parts, $path_parts[$i]);
        $j++;
    }
    return $path_root . implode("/", $real_path_parts);
}
?>
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...
-欢迎为本文评级

Random Posts

本文读者也关心以下内容:

  • N/A

07月 17th, 2008 作者: eric | 未分类 | Trackback ? | 无评论| Email This Post Print This Post | 72 views

Add a Comment

Leave a reply

No Comments