johannes.heinen@googlemail.com schrieb:
Quote:
The next step is to clean up pathnames and get rid of trailing slashes
- following your suggestion. I would like to take your rtrim solution
for getting this done, but i wonder if realpath() is able to handle
this properly and platform-independent? (I fear traps like
realpath("") =".", realpath("/") ="/" and the backslash on windows-
platforms). Do you think that there are arguments against using
realpath to filter every constructor-argument of SplFileInfo()? -
Think it may be too easy to work properly...^^
If you want to use realpath(), watch out for the following:
- determine if you are dealing with a file or a folder
- make sure the folder exists: is_dir( dirname( $file ) ), or
check the return value of realpath() accordingly
- remove any trailing slashes before calling realpath()
- don't rely on realpath() for adding/removing trailing slashes
As for removing the trailing slash in a platform-independent way:
rtrim( $file, '\/' )
or
rtrim( $file, DIRECTORY_SEPARATOR )
In your constructor, you could "clean" the filebase-root-directory like
this:
if ( !is_string( $file ) ) { throw new InvalidArgumentException; }
if ( !is_dir( $file ) ) { throw new InvalidArgumentException; }
// Take no chances with realpath() and trailing slash
$file = rtrim( realpath( rtrim( $file, '\/' ) ), '\/' );
// Append DIRECTORY_SEPARATOR if you like:
$file .= DIRECTORY_SEPARATOR;
But unless you need a canonical representation of $file, you probably
are better off without realpath(). I have written an autoloader class
using realpath(). It worked under Windows, but not under Linux -- due to
realpath()'s appending/stripping trailing slashes in its output, which
it handled differently on the two platforms. As I didn't really need it,
I removed it and now my class works on both systems.
You might also want to take a look at the glob() function:
glob( $input, GLOB_BRACE | GLOB_MARK | GLOB_ONLYDIR );
This would give you an array of all directories matching $input (if you
want all files, leave out the GLOB_ONLYDIR). This way you could specify
files like this:
'/home/whatever/{foo,bar}/*'
But then, this might not help you at all with your current project. I
hope that my remarks are somehow helpful.
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux ā avoir tort qu'ils ont raison!
(Coluche)