CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://cakephp.org CakePHP(tm) Project
 11:  * @package       Cake.Utility
 12:  * @since         CakePHP(tm) v 0.2.9
 13:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  */
 15: 
 16: /**
 17:  * Folder structure browser, lists folders and files.
 18:  * Provides an Object interface for Common directory related tasks.
 19:  *
 20:  * @package       Cake.Utility
 21:  */
 22: class Folder {
 23: 
 24: /**
 25:  * Path to Folder.
 26:  *
 27:  * @var string
 28:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$path
 29:  */
 30:     public $path = null;
 31: 
 32: /**
 33:  * Sortedness. Whether or not list results
 34:  * should be sorted by name.
 35:  *
 36:  * @var boolean
 37:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$sort
 38:  */
 39:     public $sort = false;
 40: 
 41: /**
 42:  * Mode to be used on create. Does nothing on windows platforms.
 43:  *
 44:  * @var integer
 45:  * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::$mode
 46:  */
 47:     public $mode = 0755;
 48: 
 49: /**
 50:  * Holds messages from last method.
 51:  *
 52:  * @var array
 53:  */
 54:     protected $_messages = array();
 55: 
 56: /**
 57:  * Holds errors from last method.
 58:  *
 59:  * @var array
 60:  */
 61:     protected $_errors = array();
 62: 
 63: /**
 64:  * Holds array of complete directory paths.
 65:  *
 66:  * @var array
 67:  */
 68:     protected $_directories;
 69: 
 70: /**
 71:  * Holds array of complete file paths.
 72:  *
 73:  * @var array
 74:  */
 75:     protected $_files;
 76: 
 77: /**
 78:  * Constructor.
 79:  *
 80:  * @param string $path Path to folder
 81:  * @param boolean $create Create folder if not found
 82:  * @param mixed $mode Mode (CHMOD) to apply to created folder, false to ignore
 83:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder
 84:  */
 85:     public function __construct($path = false, $create = false, $mode = false) {
 86:         if (empty($path)) {
 87:             $path = TMP;
 88:         }
 89:         if ($mode) {
 90:             $this->mode = $mode;
 91:         }
 92: 
 93:         if (!file_exists($path) && $create === true) {
 94:             $this->create($path, $this->mode);
 95:         }
 96:         if (!Folder::isAbsolute($path)) {
 97:             $path = realpath($path);
 98:         }
 99:         if (!empty($path)) {
100:             $this->cd($path);
101:         }
102:     }
103: 
104: /**
105:  * Return current path.
106:  *
107:  * @return string Current path
108:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::pwd
109:  */
110:     public function pwd() {
111:         return $this->path;
112:     }
113: 
114: /**
115:  * Change directory to $path.
116:  *
117:  * @param string $path Path to the directory to change to
118:  * @return string The new path. Returns false on failure
119:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::cd
120:  */
121:     public function cd($path) {
122:         $path = $this->realpath($path);
123:         if (is_dir($path)) {
124:             return $this->path = $path;
125:         }
126:         return false;
127:     }
128: 
129: /**
130:  * Returns an array of the contents of the current directory.
131:  * The returned array holds two arrays: One of directories and one of files.
132:  *
133:  * @param boolean $sort Whether you want the results sorted, set this and the sort property
134:  *   to false to get unsorted results.
135:  * @param mixed $exceptions Either an array or boolean true will not grab dot files
136:  * @param boolean $fullPath True returns the full path
137:  * @return mixed Contents of current directory as an array, an empty array on failure
138:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read
139:  */
140:     public function read($sort = true, $exceptions = false, $fullPath = false) {
141:         $dirs = $files = array();
142: 
143:         if (!$this->pwd()) {
144:             return array($dirs, $files);
145:         }
146:         if (is_array($exceptions)) {
147:             $exceptions = array_flip($exceptions);
148:         }
149:         $skipHidden = isset($exceptions['.']) || $exceptions === true;
150: 
151:         try {
152:             $iterator = new DirectoryIterator($this->path);
153:         } catch (Exception $e) {
154:             return array($dirs, $files);
155:         }
156: 
157:         foreach ($iterator as $item) {
158:             if ($item->isDot()) {
159:                 continue;
160:             }
161:             $name = $item->getFileName();
162:             if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
163:                 continue;
164:             }
165:             if ($fullPath) {
166:                 $name = $item->getPathName();
167:             }
168:             if ($item->isDir()) {
169:                 $dirs[] = $name;
170:             } else {
171:                 $files[] = $name;
172:             }
173:         }
174:         if ($sort || $this->sort) {
175:             sort($dirs);
176:             sort($files);
177:         }
178:         return array($dirs, $files);
179:     }
180: 
181: /**
182:  * Returns an array of all matching files in current directory.
183:  *
184:  * @param string $regexpPattern Preg_match pattern (Defaults to: .*)
185:  * @param boolean $sort Whether results should be sorted.
186:  * @return array Files that match given pattern
187:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::find
188:  */
189:     public function find($regexpPattern = '.*', $sort = false) {
190:         list($dirs, $files) = $this->read($sort);
191:         return array_values(preg_grep('/^' . $regexpPattern . '$/i', $files));
192:     }
193: 
194: /**
195:  * Returns an array of all matching files in and below current directory.
196:  *
197:  * @param string $pattern Preg_match pattern (Defaults to: .*)
198:  * @param boolean $sort Whether results should be sorted.
199:  * @return array Files matching $pattern
200:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::findRecursive
201:  */
202:     public function findRecursive($pattern = '.*', $sort = false) {
203:         if (!$this->pwd()) {
204:             return array();
205:         }
206:         $startsOn = $this->path;
207:         $out = $this->_findRecursive($pattern, $sort);
208:         $this->cd($startsOn);
209:         return $out;
210:     }
211: 
212: /**
213:  * Private helper function for findRecursive.
214:  *
215:  * @param string $pattern Pattern to match against
216:  * @param boolean $sort Whether results should be sorted.
217:  * @return array Files matching pattern
218:  */
219:     protected function _findRecursive($pattern, $sort = false) {
220:         list($dirs, $files) = $this->read($sort);
221:         $found = array();
222: 
223:         foreach ($files as $file) {
224:             if (preg_match('/^' . $pattern . '$/i', $file)) {
225:                 $found[] = Folder::addPathElement($this->path, $file);
226:             }
227:         }
228:         $start = $this->path;
229: 
230:         foreach ($dirs as $dir) {
231:             $this->cd(Folder::addPathElement($start, $dir));
232:             $found = array_merge($found, $this->findRecursive($pattern, $sort));
233:         }
234:         return $found;
235:     }
236: 
237: /**
238:  * Returns true if given $path is a Windows path.
239:  *
240:  * @param string $path Path to check
241:  * @return boolean true if windows path, false otherwise
242:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isWindowsPath
243:  */
244:     public static function isWindowsPath($path) {
245:         return (preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) == '\\\\');
246:     }
247: 
248: /**
249:  * Returns true if given $path is an absolute path.
250:  *
251:  * @param string $path Path to check
252:  * @return boolean true if path is absolute.
253:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isAbsolute
254:  */
255:     public static function isAbsolute($path) {
256:         return !empty($path) && ($path[0] === '/' || preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) == '\\\\');
257:     }
258: 
259: /**
260:  * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
261:  *
262:  * @param string $path Path to check
263:  * @return string Set of slashes ("\\" or "/")
264:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::normalizePath
265:  */
266:     public static function normalizePath($path) {
267:         return Folder::correctSlashFor($path);
268:     }
269: 
270: /**
271:  * Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
272:  *
273:  * @param string $path Path to check
274:  * @return string Set of slashes ("\\" or "/")
275:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::correctSlashFor
276:  */
277:     public static function correctSlashFor($path) {
278:         return (Folder::isWindowsPath($path)) ? '\\' : '/';
279:     }
280: 
281: /**
282:  * Returns $path with added terminating slash (corrected for Windows or other OS).
283:  *
284:  * @param string $path Path to check
285:  * @return string Path with ending slash
286:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::slashTerm
287:  */
288:     public static function slashTerm($path) {
289:         if (Folder::isSlashTerm($path)) {
290:             return $path;
291:         }
292:         return $path . Folder::correctSlashFor($path);
293:     }
294: 
295: /**
296:  * Returns $path with $element added, with correct slash in-between.
297:  *
298:  * @param string $path Path
299:  * @param string $element Element to and at end of path
300:  * @return string Combined path
301:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::addPathElement
302:  */
303:     public static function addPathElement($path, $element) {
304:         return rtrim($path, DS) . DS . $element;
305:     }
306: 
307: /**
308:  * Returns true if the File is in a given CakePath.
309:  *
310:  * @param string $path The path to check.
311:  * @return boolean
312:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inCakePath
313:  */
314:     public function inCakePath($path = '') {
315:         $dir = substr(Folder::slashTerm(ROOT), 0, -1);
316:         $newdir = $dir . $path;
317: 
318:         return $this->inPath($newdir);
319:     }
320: 
321: /**
322:  * Returns true if the File is in given path.
323:  *
324:  * @param string $path The path to check that the current pwd() resides with in.
325:  * @param boolean $reverse Reverse the search, check that pwd() resides within $path.
326:  * @return boolean
327:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::inPath
328:  */
329:     public function inPath($path = '', $reverse = false) {
330:         $dir = Folder::slashTerm($path);
331:         $current = Folder::slashTerm($this->pwd());
332: 
333:         if (!$reverse) {
334:             $return = preg_match('/^(.*)' . preg_quote($dir, '/') . '(.*)/', $current);
335:         } else {
336:             $return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
337:         }
338:         return (bool)$return;
339:     }
340: 
341: /**
342:  * Change the mode on a directory structure recursively. This includes changing the mode on files as well.
343:  *
344:  * @param string $path The path to chmod
345:  * @param integer $mode octal value 0755
346:  * @param boolean $recursive chmod recursively, set to false to only change the current directory.
347:  * @param array $exceptions array of files, directories to skip
348:  * @return boolean Returns TRUE on success, FALSE on failure
349:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::chmod
350:  */
351:     public function chmod($path, $mode = false, $recursive = true, $exceptions = array()) {
352:         if (!$mode) {
353:             $mode = $this->mode;
354:         }
355: 
356:         if ($recursive === false && is_dir($path)) {
357:             if (@chmod($path, intval($mode, 8))) {
358:                 $this->_messages[] = __d('cake_dev', '%s changed to %s', $path, $mode);
359:                 return true;
360:             }
361: 
362:             $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $path, $mode);
363:             return false;
364:         }
365: 
366:         if (is_dir($path)) {
367:             $paths = $this->tree($path);
368: 
369:             foreach ($paths as $type) {
370:                 foreach ($type as $key => $fullpath) {
371:                     $check = explode(DS, $fullpath);
372:                     $count = count($check);
373: 
374:                     if (in_array($check[$count - 1], $exceptions)) {
375:                         continue;
376:                     }
377: 
378:                     if (@chmod($fullpath, intval($mode, 8))) {
379:                         $this->_messages[] = __d('cake_dev', '%s changed to %s', $fullpath, $mode);
380:                     } else {
381:                         $this->_errors[] = __d('cake_dev', '%s NOT changed to %s', $fullpath, $mode);
382:                     }
383:                 }
384:             }
385: 
386:             if (empty($this->_errors)) {
387:                 return true;
388:             }
389:         }
390:         return false;
391:     }
392: 
393: /**
394:  * Returns an array of nested directories and files in each directory
395:  *
396:  * @param string $path the directory path to build the tree from
397:  * @param mixed $exceptions Either an array of files/folder to exclude
398:  *   or boolean true to not grab dot files/folders
399:  * @param string $type either 'file' or 'dir'. null returns both files and directories
400:  * @return mixed array of nested directories and files in each directory
401:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
402:  */
403:     public function tree($path = null, $exceptions = false, $type = null) {
404:         if ($path == null) {
405:             $path = $this->path;
406:         }
407:         $files = array();
408:         $directories = array($path);
409: 
410:         if (is_array($exceptions)) {
411:             $exceptions = array_flip($exceptions);
412:         }
413:         $skipHidden = false;
414:         if ($exceptions === true) {
415:             $skipHidden = true;
416:         } elseif (isset($exceptions['.'])) {
417:             $skipHidden = true;
418:             unset($exceptions['.']);
419:         }
420: 
421:         try {
422:             $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF);
423:             $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
424:         } catch (Exception $e) {
425:             if ($type === null) {
426:                 return array(array(), array());
427:             }
428:             return array();
429:         }
430: 
431:         foreach ($iterator as $itemPath => $fsIterator) {
432:             if ($skipHidden) {
433:                 $subPathName = $fsIterator->getSubPathname();
434:                 if ($subPathName{0} == '.' || strpos($subPathName, DS . '.') !== false) {
435:                     continue;
436:                 }
437:             }
438:             $item = $fsIterator->current();
439:             if (!empty($exceptions) && isset($exceptions[$item->getFilename()])) {
440:                 continue;
441:             }
442: 
443:             if ($item->isFile()) {
444:                 $files[] = $itemPath;
445:             } elseif ($item->isDir() && !$item->isDot()) {
446:                 $directories[] = $itemPath;
447:             }
448:         }
449:         if ($type === null) {
450:             return array($directories, $files);
451:         }
452:         if ($type === 'dir') {
453:             return $directories;
454:         }
455:         return $files;
456:     }
457: 
458: /**
459:  * Create a directory structure recursively. Can be used to create
460:  * deep path structures like `/foo/bar/baz/shoe/horn`
461:  *
462:  * @param string $pathname The directory structure to create
463:  * @param integer $mode octal value 0755
464:  * @return boolean Returns TRUE on success, FALSE on failure
465:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::create
466:  */
467:     public function create($pathname, $mode = false) {
468:         if (is_dir($pathname) || empty($pathname)) {
469:             return true;
470:         }
471: 
472:         if (!$mode) {
473:             $mode = $this->mode;
474:         }
475: 
476:         if (is_file($pathname)) {
477:             $this->_errors[] = __d('cake_dev', '%s is a file', $pathname);
478:             return false;
479:         }
480:         $pathname = rtrim($pathname, DS);
481:         $nextPathname = substr($pathname, 0, strrpos($pathname, DS));
482: 
483:         if ($this->create($nextPathname, $mode)) {
484:             if (!file_exists($pathname)) {
485:                 $old = umask(0);
486:                 if (mkdir($pathname, $mode)) {
487:                     umask($old);
488:                     $this->_messages[] = __d('cake_dev', '%s created', $pathname);
489:                     return true;
490:                 } else {
491:                     umask($old);
492:                     $this->_errors[] = __d('cake_dev', '%s NOT created', $pathname);
493:                     return false;
494:                 }
495:             }
496:         }
497:         return false;
498:     }
499: 
500: /**
501:  * Returns the size in bytes of this Folder and its contents.
502:  *
503:  * @return integer size in bytes of current folder
504:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::dirsize
505:  */
506:     public function dirsize() {
507:         $size = 0;
508:         $directory = Folder::slashTerm($this->path);
509:         $stack = array($directory);
510:         $count = count($stack);
511:         for ($i = 0, $j = $count; $i < $j; ++$i) {
512:             if (is_file($stack[$i])) {
513:                 $size += filesize($stack[$i]);
514:             } elseif (is_dir($stack[$i])) {
515:                 $dir = dir($stack[$i]);
516:                 if ($dir) {
517:                     while (false !== ($entry = $dir->read())) {
518:                         if ($entry === '.' || $entry === '..') {
519:                             continue;
520:                         }
521:                         $add = $stack[$i] . $entry;
522: 
523:                         if (is_dir($stack[$i] . $entry)) {
524:                             $add = Folder::slashTerm($add);
525:                         }
526:                         $stack[] = $add;
527:                     }
528:                     $dir->close();
529:                 }
530:             }
531:             $j = count($stack);
532:         }
533:         return $size;
534:     }
535: 
536: /**
537:  * Recursively Remove directories if the system allows.
538:  *
539:  * @param string $path Path of directory to delete
540:  * @return boolean Success
541:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::delete
542:  */
543:     public function delete($path = null) {
544:         if (!$path) {
545:             $path = $this->pwd();
546:         }
547:         if (!$path) {
548:             return null;
549:         }
550:         $path = Folder::slashTerm($path);
551:         if (is_dir($path)) {
552:             try {
553:                 $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_SELF);
554:                 $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST);
555:             } catch (Exception $e) {
556:                 return false;
557:             }
558: 
559:             foreach ($iterator as $item) {
560:                 $filePath = $item->getPathname();
561:                 if ($item->isFile() || $item->isLink()) {
562:                     if (@unlink($filePath)) {
563:                         $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
564:                     } else {
565:                         $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
566:                     }
567:                 } elseif ($item->isDir() && !$item->isDot()) {
568:                     if (@rmdir($filePath)) {
569:                         $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
570:                     } else {
571:                         $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
572:                         return false;
573:                     }
574:                 }
575:             }
576: 
577:             $path = rtrim($path, DS);
578:             if (@rmdir($path)) {
579:                 $this->_messages[] = __d('cake_dev', '%s removed', $path);
580:             } else {
581:                 $this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
582:                 return false;
583:             }
584:         }
585:         return true;
586:     }
587: 
588: /**
589:  * Recursive directory copy.
590:  *
591:  * ### Options
592:  *
593:  * - `to` The directory to copy to.
594:  * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
595:  * - `mode` The mode to copy the files/directories with.
596:  * - `skip` Files/directories to skip.
597:  *
598:  * @param mixed $options Either an array of options (see above) or a string of the destination directory.
599:  * @return boolean Success
600:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::copy
601:  */
602:     public function copy($options = array()) {
603:         if (!$this->pwd()) {
604:             return false;
605:         }
606:         $to = null;
607:         if (is_string($options)) {
608:             $to = $options;
609:             $options = array();
610:         }
611:         $options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()), $options);
612: 
613:         $fromDir = $options['from'];
614:         $toDir = $options['to'];
615:         $mode = $options['mode'];
616: 
617:         if (!$this->cd($fromDir)) {
618:             $this->_errors[] = __d('cake_dev', '%s not found', $fromDir);
619:             return false;
620:         }
621: 
622:         if (!is_dir($toDir)) {
623:             $this->create($toDir, $mode);
624:         }
625: 
626:         if (!is_writable($toDir)) {
627:             $this->_errors[] = __d('cake_dev', '%s not writable', $toDir);
628:             return false;
629:         }
630: 
631:         $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
632:         if ($handle = @opendir($fromDir)) {
633:             while (false !== ($item = readdir($handle))) {
634:                 if (!in_array($item, $exceptions)) {
635:                     $from = Folder::addPathElement($fromDir, $item);
636:                     $to = Folder::addPathElement($toDir, $item);
637:                     if (is_file($from)) {
638:                         if (copy($from, $to)) {
639:                             chmod($to, intval($mode, 8));
640:                             touch($to, filemtime($from));
641:                             $this->_messages[] = __d('cake_dev', '%s copied to %s', $from, $to);
642:                         } else {
643:                             $this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
644:                         }
645:                     }
646: 
647:                     if (is_dir($from) && !file_exists($to)) {
648:                         $old = umask(0);
649:                         if (mkdir($to, $mode)) {
650:                             umask($old);
651:                             $old = umask(0);
652:                             chmod($to, $mode);
653:                             umask($old);
654:                             $this->_messages[] = __d('cake_dev', '%s created', $to);
655:                             $options = array_merge($options, array('to' => $to, 'from' => $from));
656:                             $this->copy($options);
657:                         } else {
658:                             $this->_errors[] = __d('cake_dev', '%s not created', $to);
659:                         }
660:                     }
661:                 }
662:             }
663:             closedir($handle);
664:         } else {
665:             return false;
666:         }
667: 
668:         if (!empty($this->_errors)) {
669:             return false;
670:         }
671:         return true;
672:     }
673: 
674: /**
675:  * Recursive directory move.
676:  *
677:  * ### Options
678:  *
679:  * - `to` The directory to copy to.
680:  * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
681:  * - `chmod` The mode to copy the files/directories with.
682:  * - `skip` Files/directories to skip.
683:  *
684:  * @param array $options (to, from, chmod, skip)
685:  * @return boolean Success
686:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
687:  */
688:     public function move($options) {
689:         $to = null;
690:         if (is_string($options)) {
691:             $to = $options;
692:             $options = (array)$options;
693:         }
694:         $options = array_merge(
695:             array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()),
696:             $options
697:         );
698: 
699:         if ($this->copy($options)) {
700:             if ($this->delete($options['from'])) {
701:                 return (bool)$this->cd($options['to']);
702:             }
703:         }
704:         return false;
705:     }
706: 
707: /**
708:  * get messages from latest method
709:  *
710:  * @return array
711:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::messages
712:  */
713:     public function messages() {
714:         return $this->_messages;
715:     }
716: 
717: /**
718:  * get error from latest method
719:  *
720:  * @return array
721:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::errors
722:  */
723:     public function errors() {
724:         return $this->_errors;
725:     }
726: 
727: /**
728:  * Get the real path (taking ".." and such into account)
729:  *
730:  * @param string $path Path to resolve
731:  * @return string The resolved path
732:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
733:  */
734:     public function realpath($path) {
735:         $path = str_replace('/', DS, trim($path));
736:         if (strpos($path, '..') === false) {
737:             if (!Folder::isAbsolute($path)) {
738:                 $path = Folder::addPathElement($this->path, $path);
739:             }
740:             return $path;
741:         }
742:         $parts = explode(DS, $path);
743:         $newparts = array();
744:         $newpath = '';
745:         if ($path[0] === DS) {
746:             $newpath = DS;
747:         }
748: 
749:         while (($part = array_shift($parts)) !== null) {
750:             if ($part === '.' || $part === '') {
751:                 continue;
752:             }
753:             if ($part === '..') {
754:                 if (!empty($newparts)) {
755:                     array_pop($newparts);
756:                     continue;
757:                 } else {
758:                     return false;
759:                 }
760:             }
761:             $newparts[] = $part;
762:         }
763:         $newpath .= implode(DS, $newparts);
764: 
765:         return Folder::slashTerm($newpath);
766:     }
767: 
768: /**
769:  * Returns true if given $path ends in a slash (i.e. is slash-terminated).
770:  *
771:  * @param string $path Path to check
772:  * @return boolean true if path ends with slash, false otherwise
773:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isSlashTerm
774:  */
775:     public static function isSlashTerm($path) {
776:         $lastChar = $path[strlen($path) - 1];
777:         return $lastChar === '/' || $lastChar === '\\';
778:     }
779: 
780: }
781: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs