Jump to content

[SOLVED] Cleaning up path with php


ivalmian

Recommended Posts

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

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.