how to remove filename from path

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

how to remove filename from path

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Easiest way is to use dirname
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

thanks the function works well, but how to remove one slash from path
c://aaa//new

i need

c:/aaa/new
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Already answered... Unfortunately after you saw the initial answer.. sorry about that
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post 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/
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

thank you so much for your kind reply. Eshban
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Hmm, not tested...

Code: Select all

$str = preg_replace("/(.*\/).*/","\\1",$str);
That gets the path I think...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basename(), dirname() and possibly realpath() are your friends.
Post Reply