Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Arrays
  • Callback
  • FileSystem
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Validators

Exceptions

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