Check if directory path ends with DIRECTORY_SEPARATOR

Check if directory path ends with DIRECTORY_SEPARATOR

In PHP I’m receiving a string from the user for a local file directory Path

I want to know if they’ve included the trailing slash (/) or not.

I would like this to be cross platform so I’d like to use the PHP constant¬†DIRECTORY_SEPARATOR

preg_match("/" . preg_quote(DIRECTORY_SEPARATOR) . "$/", $path);

or

rtrim($path,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

 

Leave a Comment