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 "nnn Dirty paths:n"; $paths[] = 'C://////////Windows//////System'; // C:/System $paths[] = 'C:HTMLjavascript..examplescolors.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"" . $path . "" = "" . fixpath($path) . """; } ?> |
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); } ?> |
| -欢迎为本文评级 |
No donations within the last 180 days.Who make donation will leave message at here.Donate Now.
This a wordpress plugin Wp-Donators.It provides a smart donation function to autoleave the sponsor information in this container after payment. People can donate and submit name/URL or TextLink AD. The information of the latest donors are displayed in the cloud. The more a person donates, the bigger their link will be.It's will support most popular payment interface in future. ParPal Just the first one. More..
Powered By:WP-DONATORS
相关日志 |
本文读者也关心以下内容:
|















































Leave a reply