how to remove filename from path
Moderator: General Moderators
how to remove filename from path
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
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
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Easiest way is to use dirname
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Oh and to replace the slash you can use str_replace('\\','\',$filename); or an even better way
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.
Code: Select all
$filename=str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$filename);- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
thankyou so much
i use
but it not accept single slash
my desired output is:
i.e
C:/phpdev/www/test/
i use
Code: Select all
$newpath = str_replace('\\','"\"',dirname($_REQUEST['path_or']));my desired output is:
i.e
C:/phpdev/www/test/
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
'\' is an escape character. Forgot that one... You will probably need or use the directory separator as previously suggested.
Code: Select all
str_replace('\\\\','\\',dirname($_REQUEST['path_or']));- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Hmm, not tested...
That gets the path I think...
Code: Select all
$str = preg_replace("/(.*\/).*/","\\1",$str);