PHP in Windows: Comparing Short & Long Names

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
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

PHP in Windows: Comparing Short & Long Names

Post by jeff00seattle »

Hi

What is the best approach in determining if two Windows file or directory locations are the same?

In Windows, file and directory paths can be given in either long or short names.

A long file name is considered to be any file name that exceeds the short MS-DOS (also called 8.3) style naming convention.
See: MSDN Short vs. Long Names

My issue is trying to find a PHP way of comparing if a long name and a short name are the same location.

For example, these are the same directory locations:
Short name: C:\Users\A-JETA~1.SEA\AppData\Local\Temp\
Long name: C:\Users\a-jetann.SEATTLE\AppData\Local\Temp\

I tried to be tricky with the following code, but this did not work:

Code: Select all

$oDirShort = dir($strDirShort);
$oDirLong  = dir($strDirLong);
 
$strDirShortPath = $oDirShort->path;
$strDirLongPath  = $oDirLong->path;
 
return strcasecmp($strDirShortPath, $strDirLongPath) == 0;
Thanks

Jeff in Seattle
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP in Windows: Comparing Short & Long Names

Post by requinix »

I'd compare the two after using realpath().
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: PHP in Windows: Comparing Short & Long Names

Post by jeff00seattle »

Thanks!

Code: Select all

function isDirSame( $strDirA, $strDirB )
{
  if ( !is_dir($strDirA) ) {
    die( "Error: Directory \"{$strDirA}\" not exists!");
  }
  if ( !is_dir($strDirB) ) {
    die( "Error: Directory \"{$strDirB}\" not exists!");
  }
  
  $strRealDirA = [color=#0000FF]realpath[/color]($strDirA);
  $strRealDirB = [color=#0000FF]realpath[/color]($strDirB);
  
  return strcasecmp($strRealDirA, $strRealDirB) == 0;
}
Post Reply