Namespaces

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

Classes

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

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • 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: class FileSystem
 17: {
 18: 
 19:     /**
 20:      * Creates a directory.
 21:      * @return void
 22:      * @throws Nette\IOException
 23:      */
 24:     public static function createDir($dir, $mode = 0777)
 25:     {
 26:         if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE) && !is_dir($dir)) { // @ - dir may already exist
 27:             $error = error_get_last();
 28:             throw new Nette\IOException("Unable to create directory '$dir'. $error[message]");
 29:         }
 30:     }
 31: 
 32: 
 33:     /**
 34:      * Copies a file or directory.
 35:      * @return void
 36:      * @throws Nette\IOException
 37:      */
 38:     public static function copy($source, $dest, $overwrite = TRUE)
 39:     {
 40:         if (stream_is_local($source) && !file_exists($source)) {
 41:             throw new Nette\IOException("File or directory '$source' not found.");
 42: 
 43:         } elseif (!$overwrite && file_exists($dest)) {
 44:             throw new Nette\InvalidStateException("File or directory '$dest' already exists.");
 45: 
 46:         } elseif (is_dir($source)) {
 47:             static::createDir($dest);
 48:             foreach (new \FilesystemIterator($dest) as $item) {
 49:                 static::delete($item);
 50:             }
 51:             foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
 52:                 if ($item->isDir()) {
 53:                     static::createDir($dest . '/' . $iterator->getSubPathName());
 54:                 } else {
 55:                     static::copy($item, $dest . '/' . $iterator->getSubPathName());
 56:                 }
 57:             }
 58: 
 59:         } else {
 60:             static::createDir(dirname($dest));
 61:             if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === FALSE) { // @ is escalated to exception
 62:                 throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
 63:             }
 64:         }
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Deletes a file or directory.
 70:      * @return void
 71:      * @throws Nette\IOException
 72:      */
 73:     public static function delete($path)
 74:     {
 75:         if (is_file($path) || is_link($path)) {
 76:             $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
 77:             if (!@$func($path)) { // @ is escalated to exception
 78:                 throw new Nette\IOException("Unable to delete '$path'.");
 79:             }
 80: 
 81:         } elseif (is_dir($path)) {
 82:             foreach (new \FilesystemIterator($path) as $item) {
 83:                 static::delete($item);
 84:             }
 85:             if (!@rmdir($path)) { // @ is escalated to exception
 86:                 throw new Nette\IOException("Unable to delete directory '$path'.");
 87:             }
 88:         }
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Renames a file or directory.
 94:      * @return void
 95:      * @throws Nette\IOException
 96:      * @throws Nette\InvalidStateException if the target file or directory already exist
 97:      */
 98:     public static function rename($name, $newName, $overwrite = TRUE)
 99:     {
100:         if (!$overwrite && file_exists($newName)) {
101:             throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
102: 
103:         } elseif (!file_exists($name)) {
104:             throw new Nette\IOException("File or directory '$name' not found.");
105: 
106:         } else {
107:             static::createDir(dirname($newName));
108:             static::delete($newName);
109:             if (!@rename($name, $newName)) { // @ is escalated to exception
110:                 throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
111:             }
112:         }
113:     }
114: 
115: 
116:     /**
117:      * Reads file content.
118:      * @return string
119:      * @throws Nette\IOException
120:      */
121:     public static function read($file)
122:     {
123:         $content = @file_get_contents($file); // @ is escalated to exception
124:         if ($content === FALSE) {
125:             throw new Nette\IOException("Unable to read file '$file'.");
126:         }
127:         return $content;
128:     }
129: 
130: 
131:     /**
132:      * Writes a string to a file.
133:      * @return void
134:      * @throws Nette\IOException
135:      */
136:     public static function write($file, $content, $mode = 0666)
137:     {
138:         static::createDir(dirname($file));
139:         if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
140:             throw new Nette\IOException("Unable to write file '$file'.");
141:         }
142:         if ($mode !== NULL && !@chmod($file, $mode)) { // @ is escalated to exception
143:             throw new Nette\IOException("Unable to chmod file '$file'.");
144:         }
145:     }
146: 
147: 
148:     /**
149:      * Is path absolute?
150:      * @return bool
151:      */
152:     public static function isAbsolute($path)
153:     {
154:         return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
155:     }
156: 
157: }
158: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0