Page 1 of 1
how to remove filename from path
Posted: Wed Dec 13, 2006 3:38 am
by eshban
I have a path like
C:\\phpdev\\www\\test\\test2.html
i want to remove test.html(filename) and single slash and i just need
C:\phpdev\www\test\
how can i do it
Posted: Wed Dec 13, 2006 3:42 am
by CoderGoblin
Easiest way is to use
dirname
Posted: Wed Dec 13, 2006 3:50 am
by CoderGoblin
Oh and to replace the slash you can use
str_replace('\\','\',$filename); or an even better way
Code: Select all
$filename=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$filename);
DIRECTORY_SEPARATOR is an inbuilt constant which shows the separator based on your server platform \ for windows / for Linux etc. Can't remember where to find it in the manual.
Posted: Wed Dec 13, 2006 3:51 am
by eshban
thanks the function works well, but how to remove one slash from path
c://aaa//new
i need
c:/aaa/new
Posted: Wed Dec 13, 2006 3:53 am
by CoderGoblin
Already answered... Unfortunately after you saw the initial answer.. sorry about that
Posted: Wed Dec 13, 2006 3:59 am
by eshban
thankyou so much
i use
Code: Select all
$newpath = str_replace('\\','"\"',dirname($_REQUEST['path_or']));
but it not accept single slash
my desired output is:
i.e
C:/phpdev/www/test/
Posted: Wed Dec 13, 2006 4:05 am
by CoderGoblin
'\' is an escape character. Forgot that one... You will probably need
Code: Select all
str_replace('\\\\','\\',dirname($_REQUEST['path_or']));
or use the directory separator as previously suggested.
Posted: Wed Dec 13, 2006 4:19 am
by eshban
thank you so much for your kind reply. Eshban
Posted: Wed Dec 13, 2006 5:55 am
by kaisellgren
Hmm, not tested...
Code: Select all
$str = preg_replace("/(.*\/).*/","\\1",$str);
That gets the path I think...
Posted: Wed Dec 13, 2006 7:43 am
by feyd
basename(),
dirname() and possibly
realpath() are your friends.