Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • CallbackFilterIterator
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • LimitedScope
  • MimeTypeDetector
  • ObjectMixin
  • Paginator
  • Random
  • RecursiveCallbackFilterIterator
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * File system tool.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class FileSystem
 19: {
 20: 
 21:     /**
 22:      * Creates a directory.
 23:      * @return void
 24:      */
 25:     public static function createDir($dir, $mode = 0777)
 26:     {
 27:         if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE)) { // intentionally @; not atomic
 28:             throw new Nette\IOException("Unable to create directory '$dir'.");
 29:         }
 30:     }
 31: 
 32: 
 33:     /**
 34:      * Copies a file or directory.
 35:      * @return void
 36:      */
 37:     public static function copy($source, $dest, $overwrite = TRUE)
 38:     {
 39:         if (stream_is_local($source) && !file_exists($source)) {
 40:             throw new Nette\IOException("File or directory '$source' not found.");
 41: 
 42:         } elseif (!$overwrite && file_exists($dest)) {
 43:             throw new Nette\InvalidStateException("File or directory '$dest' already exists.");
 44: 
 45:         } elseif (is_dir($source)) {
 46:             static::createDir($dest);
 47:             foreach (new \FilesystemIterator($dest) as $item) {
 48:                 static::delete($item);
 49:             }
 50:             foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
 51:                 if ($item->isDir()) {
 52:                     static::createDir($dest . '/' . $iterator->getSubPathName());
 53:                 } else {
 54:                     static::copy($item, $dest . '/' . $iterator->getSubPathName());
 55:                 }
 56:             }
 57: 
 58:         } else {
 59:             static::createDir(dirname($dest));
 60:             if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === FALSE) {
 61:                 throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
 62:             }
 63:         }
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Deletes a file or directory.
 69:      * @return void
 70:      */
 71:     public static function delete($path)
 72:     {
 73:         if (is_file($path) || is_link($path)) {
 74:             $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
 75:             if (!@$func($path)) {
 76:                 throw new Nette\IOException("Unable to delete '$path'.");
 77:             }
 78: 
 79:         } elseif (is_dir($path)) {
 80:             foreach (new \FilesystemIterator($path) as $item) {
 81:                 static::delete($item);
 82:             }
 83:             if (!@rmdir($path)) {
 84:                 throw new Nette\IOException("Unable to delete directory '$path'.");
 85:             }
 86:         }
 87:     }
 88: 
 89: 
 90:     /**
 91:      * Renames a file or directory.
 92:      * @return void
 93:      */
 94:     public static function rename($name, $newName, $overwrite = TRUE)
 95:     {
 96:         if (!$overwrite && file_exists($newName)) {
 97:             throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
 98: 
 99:         } elseif (!file_exists($name)) {
100:             throw new Nette\IOException("File or directory '$name' not found.");
101: 
102:         } else {
103:             static::createDir(dirname($newName));
104:             static::delete($newName);
105:             if (!@rename($name, $newName)) {
106:                 throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
107:             }
108:         }
109:     }
110: 
111: 
112:     /**
113:      * Writes a string to a file.
114:      * @return bool
115:      */
116:     public static function write($file, $content, $mode = 0666)
117:     {
118:         static::createDir(dirname($file));
119:         if (@file_put_contents($file, $content) === FALSE) {
120:             throw new Nette\IOException("Unable to write file '$file'.");
121:         }
122:         if ($mode !== NULL && !@chmod($file, $mode)) {
123:             throw new Nette\IOException("Unable to chmod file '$file'.");
124:         }
125:     }
126: 
127: 
128:     /**
129:      * Is path absolute?
130:      * @return bool
131:      */
132:     public static function isAbsolute($path)
133:     {
134:         return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
135:     }
136: 
137: }
138: 
Nette 2.2.6 API API documentation generated by ApiGen 2.8.0