Forum Moderators: coopster

Message Too Old, No Replies

reading an absolute filepath

filepaths

         

Matthew1980

7:57 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello there,

I would like to know if i am approaching this wrong (which i think i am).

I would like to take an absolute path ie: c:/path/to/files/ and use a function to read the path and check between the "/" to see how many dir's are in the filetree.

I am thinking i can use ereg or ereg_replace to check for patterns between the '/' and then count the amount of '/' returned but i think that i have got the syntax wrong:-

ereg_replace("^/[a-z]¦[0-9]/$","/",$path);

Any help is greatly appreciated - this is just a curiosity for the most part though.

thank you,

MRB

idev

9:29 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Hello Matthew,

No need to use ereg for that.


<?php

$s = 'c:/path/to/files/';

$folders = explode('/', $s);

$num = 0;

foreach($folders as $folder)
if((strlen($folder)>0) && strpos($folder,':')===false) $num++;

echo $num;

idev

9:34 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



actually, with a quick thought, it looks like


<?php

$s = 'c:/path/to/files/';
$s = ereg_replace('([^/]¦/$)','',$s);
echo strlen($S);

Matthew1980

10:07 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Guys,

Thank you for the quick replys, i will have a bash at both of these suggestions. Something to keep me busy anyway!

Cheers,

MRB

coopster

12:13 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Matthew1980.
Welcome to WebmasterWorld, idev.

explode [php.net] and count [php.net] would probably be quickest and easiest. It will also allow you to play around with the path "parts" a bit if necessary. An example you can use to learn ...

$paths = array( 
'path',
'C:/this/is/the/path',
'C:/this/is/the/path/',
'/this/is/the/path',
'/this/is/the/path/'
);
print '<pre>';
foreach ($paths as $path) {
$p = explode('/', $path);
print count($p) . ': ' . htmlentities($path) . "\n";
$p = array_values(array_filter($p));
print count($p) . ': ' . htmlentities(implode('/', $p)) . "\n";
if (preg_match('/^[a-z]:/i', $p[0])) {
unset($p[0]);
print count($p) . ': ' . htmlentities(implode('/', $p)) . "\n";
}
print "\n";
}
print '</pre>';
exit;

array_filter [php.net] gets rid of any "blank" path parts, such as those before the root and after the last path, as in:

(here)/this/is/the/path/(and here)

The array_values [php.net] is present merely to reset the numerical sequence of the array after filtering out empty indexes.

Finally, the regular expression is looking for any Windows OS root. Any first path (array index zero) that starts with a letter followed by a colon, case-insensitive.

Matthew1980

7:50 pm on Mar 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Coopster,

Wow, thanks for the help. I have figured out what i want to get out of this now, basically, it involves filepaths as you have guessed. My intention is to write a function that takes two parameters, firstly a constant value (the path the the root directory) and then either '+1' or '-1' the amount of directorys in the file tree. Hopefully this will negate the need for using '../' everytime i want to access a folder.

My apologies for going on a bit - hopefully i have made myself a little clearer ;-)

Cheers,

MRB