ivalmian Posted July 3, 2007 Share Posted July 3, 2007 Hi, I'm writing a file manager for a school org I'm in. My question is this, currently, when I have someone navigate through directories and they want to go one up I simply append ../ to the directory path. Since this is done in a series of places and it is fairly convenient, I don't think I'm going to change this. The problem, however, is that after some browsing you end up with a path along the lines of "../dev/sample1/../sample2/../sample3/../" instead of simply "../dev/". This becomes very problematic if one of the folders I've previously browsed was deleted (in which case the whole algorithm breaks). What I need to do is create a function that would take in the currentpath $curpath (which can be ugly and long) and spit out the optimized path (so, to return back to my example, I want to be able to pass the function "../dev/sample1/../sample2/../sample3/../" and have it return "../dev/"). I tried to make an algorithm by splitting the path by split($curpath,"/") and then removing parts of the array that precede ".." but it doesn't really work. Hope you can help me, Thank you in advance, Ilya V Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/ Share on other sites More sharing options...
sasa Posted July 3, 2007 Share Posted July 3, 2007 try <?php $a = '../dev/sample1/../sample2/../sample3/../'; $a = explode('/',$a); $count = count($a); for ($i = 1; $i < $count; $i++) { if ($a[$i] == '..') { unset($a[$i]); unset($a[$i - 1]); } } echo $a = implode('/',$a); ?> or <?php function opti_path($path, $is_opti = false) { if ($is_opti) return $path; $path = explode('/',$path); $count = count($path); $is_opti = true; for ($i = 1; $i < $count; $i++) { if ($path[$i] == '..') { if (isset($path[$i-1])) { unset($path[$i]); unset($path[$i - 1]); $is_opti = false; } } } return opti_path(implode('/',$path), $is_opti); } $a = '../dev/sample1/../../sample2/../sample3/'; echo opti_path($a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/#findComment-289048 Share on other sites More sharing options...
TreeNode Posted July 3, 2007 Share Posted July 3, 2007 It seems as if the solution is more in re-writing how you define the current working directory while you navigate instead of parsing out the required information every time. Maybe you could just keep a record (variable) of the current working directory alongside the ridiculous path string you are building? Just some ideas. Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/#findComment-289076 Share on other sites More sharing options...
ivalmian Posted July 3, 2007 Author Share Posted July 3, 2007 I actually like sasa's solution and will try to implement it... Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/#findComment-289240 Share on other sites More sharing options...
ivalmian Posted July 3, 2007 Author Share Posted July 3, 2007 Ok, while you still get some problems, the solution is actually pretty nice. Thank you, topic solved. I used, based on sasa's suggestion: function OptimizeCurpath($curpath){ $a=$curpath; $a = explode('/',$a); $count = count($a); for ($i = 1; $i < $count; $i++) { if ($a[$i] == '..' && $a[$i-1]!='..') { unset($a[$i]); unset($a[$i - 1]); } } $a=implode('/',$a); return $a; } Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/#findComment-289241 Share on other sites More sharing options...
sasa Posted July 4, 2007 Share Posted July 4, 2007 this solution don't work 100% if you have in path part '/../../' (two directory up) optimized path is wrong look <?php function OptimizeCurpath($curpath){ $a=$curpath; $a = explode('/',$a); $count = count($a); for ($i = 1; $i < $count; $i++) { if ($a[$i] == '..' && $a[$i-1]!='..') { unset($a[$i]); unset($a[$i - 1]); } } $a=implode('/',$a); return $a; } function OptimizeCurpath1($curpath){ $x = ''; while (($curpath = preg_replace('/\/[^\/\.]+\/\.\.\//','/',$curpath)) != $x) $x = $curpath; return $curpath; } function opti_path($path, $is_opti = false) { if ($is_opti) return $path; $path = explode('/',$path); $count = count($path); $is_opti = true; for ($i = 1; $i < $count; $i++) { if ($path[$i] == '..') { if (isset($path[$i-1])) { unset($path[$i]); unset($path[$i - 1]); $is_opti = false; } } } return opti_path(implode('/',$path), $is_opti); } $a = '../dev/sample1/../sample2/subsample2_1/../../sample3/../'; echo OptimizeCurpath($a); echo "\n<hr />\n"; echo opti_path($a); echo "\n<hr />\n"; echo OptimizeCurpath1($a); ?> you can see 3th possibility for do that Quote Link to comment https://forums.phpfreaks.com/topic/58291-solved-cleaning-up-path-with-php/#findComment-289379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.