note 72254 added to function.basename
| From: | jonmsawyer at gmail dot com | Date: | Tue, 09 Jan 2007 20:23:58 +0000 |
| Subject: | note 72254 added to function.basename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121502@lists.php.net to get a copy of this message | ||
@antrik at users dot sf dot net
> 15-Nov-2004 10:40
> When using basename() on a path to a directory
> ('/bar/foo/'), the last path component ('foo') is returned,
> instead of the empty string one would expect. (Both PHP
> 4.1.2 and 4.3.8 on GNU/Linux.) No idea whether this is
> considered a bug or a feature -- I found it extremely
> annoying. Had to work around using: <?php
> $file=substr($path, -1)=='/'?'':basename($path)
> ?> Watch out!
There is a reason behind this -- and it has nothing to do with being a feature. PHP was heavily
modeled off of the POSIX model. Many of the same functions you see in PHP are also in C, C++, and
Java. These languages are modeled on POSIX as well.
The directory '/bar/foo/', when passed into the function basename(), will output
'foo' because *everything*, including directories, in the POSIX model, is a _file_. Most
unix platforms, and all Windows platforms are (some Linux distributions are not) fully compliant to
the POSIX model.
For example, the device file that contains information about your harddisk, in Linux, is probably
stored in the _file_ /dev/hda.
Another example is that when you want to list information about your CPU or Memory using the Linux
kernel, you might read the _file_ /proc/cpu/info.
Directories are no exception. Directories are no more different than your regular text file -- other
than the fact that they describe a _file_-list of all files under it, and where the OS can access
them. This means that even directories treat other directories as files.
The reason why we are made to think that directories are not files is because the kernel (the OS)
simply treats these culprits differently. Your OS is lying to you! When you try to open up
c:\windows in Notepad, you simply get a runaround because the Windows operating system knows it is a
directory and knows how to treat it -- and knowing this it will not let you open it up for editing.
For if you did that, you would probably lose the data in that directory. If you are familiar with C
programming, you will know that if you lose information about a pointer to an object, the object
gets lost in memory. The same would happen if you modified a directory in the wrong way. This is why
the operating system protects its directories with the upmost care. (Some do anyway, hehe)
So when doing any kind of programming in PHP, C/++, Java, Ada, Perl, Python, Ruby, FORTRAN, and yes,
even RPG IV (for all of you AS/400 folks out there working on the IFS), you must treat directories
as files well.
This is why 'foo' is returned. For more information on POSIX, see http://en.wikipedia.org/wiki/POSIX
I hope this helps. Cheers.
----
Server IP: 64.71.164.2
Probable Submitter: 198.99.24.125 (proxied: 1.1 URSAMAJOR)
----
Manual Page -- http://www.php.net/manual/en/function.basename.php
Edit -- https://master.php.net/note/edit/72254
Del: integrated -- https://master.php.net/note/delete/72254/integrated
Del: useless -- https://master.php.net/note/delete/72254/useless
Del: bad code -- https://master.php.net/note/delete/72254/bad+code
Del: spam -- https://master.php.net/note/delete/72254/spam
Del: non-english -- https://master.php.net/note/delete/72254/non-english
Del: in docs -- https://master.php.net/note/delete/72254/in+docs
Del: other reasons-- https://master.php.net/note/delete/72254
Reject -- https://master.php.net/note/reject/72254
Search -- https://master.php.net/manage/user-notes.php