public
Last active

Unicode/UTF-8 filesystem access on MS-Windows

  • Download Gist
WinfsUtf8.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
<?php /****************** vi: set fenc=utf-8 ts=4 sw=4 et: *****************
*
* Copyright : (C) 2012 Nicolas Grekas. All rights reserved.
* Email : p@tchwork.org
* License : http://apache.org/licenses/LICENSE-2.0
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Apache License version 2.
*
***************************************************************************/
 
// This code is obsolete
// Please see https://github.com/nicolas-grekas/Patchwork-UTF8/blob/lab-windows-fs/class/Patchwork/Utf8/WinFsStreamWrapper.php
// for an up to date version, packaged as a unit testes PHP stream wrapper.
 
namespace Patchwork\PHP\Override;
 
/**
* Unicode aware filesystem access on MS-Windows.
*
* Based on COM Scripting.FileSystemObject object and 8.3 ShortPaths.
* See also comments on http://www.rooftopsolutions.nl/blog/filesystem-encoding-and-php
*
* Experimental proof of concept only.
*/
class WinfsUtf8
{
protected static $DIR;
 
 
static function hide($file)
{
self::getFs()->GetFile($file)->Attributes |= 2; // Set hidden attribute
}
 
static function absPath($f)
{
$f = strtr($f, '/', '\\');
 
if (isset($f[0]))
{
if ('/' === $f[0] || '\\' === $f[0]) return $f;
if (false !== strpos($f, ':')) return $f;
}
 
return getcwd() . '\\' . $f;
}
 
static function ls($dir)
{
try
{
$f = array('.', '..');
 
$dir = self::getFs()->getFolder(self::absPath($dir));
 
foreach ($dir->SubFolders() as $v) $f[] = $v->Name;
foreach ($dir->Files as $v) $f[] = $v->Name;
}
catch (\Exception $f)
{
$f = array();
}
 
unset($dir);
 
return $f;
}
 
static function ShortPath($f)
{
$FS = self::getFs();
$a = self::absPath($f);
 
try
{
if ($FS->FileExists($a) ) return $FS->GetFile ($a)->ShortPath;
if ($FS->FolderExists($a)) return $FS->GetFolder($a)->ShortPath;
}
catch (com_exception $e) {}
 
return $f;
}
 
 
static function chgrp($f, $group) {return chgrp(self::ShortPath($f), $group);}
static function chmod($f, $mode) {return chmod(self::ShortPath($f), $mode);}
static function chown($f, $user) {return chown(self::ShortPath($f), $user);}
 
static function copy($from, $to, $context = null)
{
if ($context || !self::getFs()->FileExists(self::absPath($from)))
{
return copy($from, $to, $context);
}
 
try
{
self::getFs()->CopyFile(self::absPath($from), self::absPath($to), true);
return true;
}
catch (com_exception $e)
{
return false;
}
}
 
static function file_exists($f)
{
$f = self::absPath($f);
return self::getFs()->FileExists($f) || self::getFs()->FolderExists($f);
}
 
static function file_get_contents($f, $use_include_path = false, $context = null, $offset = 0, $maxlen = null)
{
if (null === $maxlen) return file_get_contents(self::ShortPath($f), $use_include_path, $context, $offset);
else return file_get_contents(self::ShortPath($f), $use_include_path, $context, $offset, $maxlen);
}
 
static function file_put_contents($f, $data, $flags = 0, $context = null)
{
try {self::getFs()->CreateTextFile(self::absPath($f), false)->Close();}
catch (com_exception $e) {}
 
if (null === $context) return file_put_contents(self::ShortPath($f), $data, $flags);
else return file_put_contents(self::ShortPath($f), $data, $flags, $context);
}
 
static function file($f, $flags = 0, $context = null)
{
if (null === $context) return file(self::ShortPath($f), $flags);
else return file(self::ShortPath($f), $flags, $context);
}
 
static function fileatime($f) {return fileatime(self::ShortPath($f));}
static function filectime($f) {return filectime(self::ShortPath($f));}
static function filegroup($f) {return filegroup(self::ShortPath($f));}
static function fileinode($f) {return fileinode(self::ShortPath($f));}
static function filemtime($f) {return filemtime(self::ShortPath($f));}
static function fileowner($f) {return fileowner(self::ShortPath($f));}
static function fileperms($f) {return fileperms(self::ShortPath($f));}
static function filesize($f) {return filesize (self::ShortPath($f));}
static function filetype($f) {return filetype (self::ShortPath($f));}
 
static function fopen($f, $mode, $use_include_path = false, $context = null)
{
switch ($m = substr($mode, 0, 1))
{
case 'x': $mode[0] = 'w';
case 'w':
case 'a':
try {self::getFs()->CreateTextFile(self::absPath($f), false)->Close();}
catch (com_exception $e)
{
if ('x' === $m) return false;
}
}
 
return null === $context
? fopen(self::ShortPath($f), $mode, $use_include_path)
: fopen(self::ShortPath($f), $mode, $use_include_path, $context);
}
 
// static function glob($f) {return glob($f);}
 
static function is_dir($f) {return is_dir (self::ShortPath($f));}
static function is_executable($f) {return is_executable(self::ShortPath($f));}
static function is_file($f) {return is_file (self::ShortPath($f));}
static function is_readable($f) {return is_readable (self::ShortPath($f));}
static function is_writable($f) {return is_writable (self::ShortPath($f));}
static function is_writeable($f) {return is_writeable (self::ShortPath($f));}
 
static function mkdir($dir, $mode = 0777, $recursive = false, $context = null)
{
return mkdir($dir, $mode, $recursive);
 
if (null !== $context) return mkdir($dir, $mode, $recursive, $context);
 
$a = self::absPath($dir);
 
if ($recursive && 0)
{
$a = explode('\\', $a);
 
$pre = $a[0];
array_shift($a);
 
$b = array();
 
foreach ($a as $a)
{
if (!isset($a[0]) || '.' === $a) continue;
if ('..' === $a) $b && array_pop($b);
else $b[]= $a;
}
 
$a = $pre . implode('\\', $b);
 
$b = array();
 
while (!self::getFs()->FolderExists(dirname($a)))
{
//TODO
}
}
 
try
{
self::getFs()->CreateFolder($a);
return true;
}
catch (com_exception $e) {}
 
return mkdir($dir, $mode, $recursive);
}
 
static function parse_ini_file($f, $process_sections = false) {return parse_ini_file(self::ShortPath($f), $process_sections);}
 
static function readfile($f, $use_include_path = false, $context = null)
{
return null === $context
? readfile(self::ShortPath($f), $use_include_path)
: readfile(self::ShortPath($f), $use_include_path, $context);
}
 
static function realpath($f) {return self::file_exists($f) ? self::getFs()->GetAbsolutePathName(self::absPath($f)) : false;}
 
static function rename($from, $to, $context = null)
{
if ($context) return rename($from, $to, $context);
 
$FS = self::getFs();
$from = self::absPath($from);
$to = self::absPath($to);
 
if ($FS->FileExists($to))
{
try
{
$FS->DeleteFile($to, true);
}
catch (com_exception $e) {}
}
else if ($FS->FolderExists($to))
{
return false;
}
 
try
{
if ($FS->FileExists($from))
{
$FS->MoveFile($from, $to);
return true;
}
 
if ($FS->FolderExists($from))
{
$FS->MoveFolder($from, $to);
return true;
}
}
catch (com_exception $e) {}
 
return false;
}
 
static function rmdir($f, $context = null)
{
return null === $context
? rmdir(self::ShortPath($f))
: rmdir(self::ShortPath($f), $context);
}
 
static function stat($f) {return stat(self::ShortPath($f));}
 
static function touch($f, $time = null, $atime = null)
{
try {self::getFs()->CreateTextFile(self::absPath($f), false)->Close();}
catch (com_exception $e) {}
 
return touch(self::ShortPath($f), $time, $atime);
}
 
static function unlink($f, $context = null)
{
return null === $context
? unlink(self::ShortPath($f))
: unlink(self::ShortPath($f), $context);
}
 
static function dir($f)
{
return self::getFs()->FolderExists(self::absPath($f)) ? new WinfsUtf8Directory($f) : dir($f);
}
 
static function closedir($d = null)
{
null === $d && $d = self::$DIR;
return $d instanceof WinfsUtf8Directory ? $d->close() : closedir($d);
}
 
static function opendir($f, $context = null)
{
return self::$DIR = !$context && self::getFs()->FolderExists(self::absPath($f)) ? new WinfsUtf8Directory($f) : opendir($f, $context);
}
 
static function readdir($d = null)
{
null === $d && $d = self::$DIR;
return $d instanceof WinfsUtf8Directory ? $d->read() : readdir($d);
}
 
static function rewinddir($d = null)
{
null === $d && $d = self::$DIR;
return $d instanceof WinfsUtf8Directory ? $d->rewind() : rewinddir($d);
}
 
static function scandir($f, $sorting_order = 0, $context = null)
{
if (null !== $context) return scandir($f, $sorting_order, $context);
 
$c = self::ls($f);
 
if (!$c) return scandir($f);
 
sort($c);
 
return $c;
}
 
/*
static function popen($f) {return popen(self::ShortPath($f));}
static function exec($f) {return exec(self::ShortPath($f));}
static function passthru($f) {return passthru(self::ShortPath($f));}
static function proc_open($f) {return proc_open(self::ShortPath($f));}
static function shell_exec($f) {return shell_exec(self::ShortPath($f));}
static function ` `($f) {return ` `(self::ShortPath($f));}
static function system($f) {return system(self::ShortPath($f));}
*/
 
protected static function getFs()
{
static $FS;
isset($FS) || $FS = new \COM('Scripting.FileSystemObject', null, CP_UTF8);
return $FS;
}
}
 
class WinfsUtf8Directory extends \Directory
{
public $path, $handle;
 
protected $childs = array();
 
function __construct($path)
{
$this->path = $path;
$this->handle = $this;
$this->childs = WinfsUtf8::ls($path);
 
if (!$this->childs)
{
$this->childs = scandir($path);
$this->childs || $this->childs = array();
}
}
 
function read()
{
return (list(, $c) = each($this->childs)) ? $c : false;
}
 
function rewind()
{
reset($this->childs);
}
 
function close()
{
unset($this->path, $this->handle, $this->childs);
}
}

Saved the day! Thank you... You may want to add md5_file by the way...

Hmm... I just happen to learn that on some specific cases the shortpath might include non us-ascii characters, although it's not possible according to MS documentation.

In such a case shorthand functions are not working. (i.e. filemtime). Here's how I implemented filemtime:

    static function filemtime($f) {
        $a = self::absPath($f);
        $file = self::is_dir($f) ? self::getFs()->GetFolder($a) : self::getFs()->GetFile($a);

        $dateLastModified = $file->DateLastModified;
        return variant_date_to_timestamp($dateLastModified);
    }

Finally some update,

the code is now unit tested and included in a lab branch of Patchwork/Utf-8:
https://github.com/nicolas-grekas/Patchwork-UTF8/blob/lab-windows-fs/class/Patchwork/Utf8/WinFsStreamWrapper.php

It is now a PHP stream wrapper, a lot easier to use!

Still based on short path because I didn't find a way to "fopen() & co." binary files without it.

@ozanhazer thank you for your hint anyway, I may find a way to remove short paths one day... (or someone else please :) )

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.