wp/wp-admin/includes/class-wp-filesystem-direct.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   314 
   314 
   315 		return $rtval;
   315 		return $rtval;
   316 	}
   316 	}
   317 
   317 
   318 	/**
   318 	/**
   319 	 * Moves a file.
   319 	 * Moves a file or directory.
       
   320 	 *
       
   321 	 * After moving files or directories, OPcache will need to be invalidated.
       
   322 	 *
       
   323 	 * If moving a directory fails, `copy_dir()` can be used for a recursive copy.
       
   324 	 *
       
   325 	 * Use `move_dir()` for moving directories with OPcache invalidation and a
       
   326 	 * fallback to `copy_dir()`.
   320 	 *
   327 	 *
   321 	 * @since 2.5.0
   328 	 * @since 2.5.0
   322 	 *
   329 	 *
   323 	 * @param string $source      Path to the source file.
   330 	 * @param string $source      Path to the source file.
   324 	 * @param string $destination Path to the destination file.
   331 	 * @param string $destination Path to the destination file.
   329 	public function move( $source, $destination, $overwrite = false ) {
   336 	public function move( $source, $destination, $overwrite = false ) {
   330 		if ( ! $overwrite && $this->exists( $destination ) ) {
   337 		if ( ! $overwrite && $this->exists( $destination ) ) {
   331 			return false;
   338 			return false;
   332 		}
   339 		}
   333 
   340 
       
   341 		if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination, true ) ) {
       
   342 			// Can't overwrite if the destination couldn't be deleted.
       
   343 			return false;
       
   344 		}
       
   345 
   334 		// Try using rename first. if that fails (for example, source is read only) try copy.
   346 		// Try using rename first. if that fails (for example, source is read only) try copy.
   335 		if ( @rename( $source, $destination ) ) {
   347 		if ( @rename( $source, $destination ) ) {
   336 			return true;
   348 			return true;
   337 		}
   349 		}
   338 
   350 
   339 		if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
   351 		// Backward compatibility: Only fall back to `::copy()` for single files.
       
   352 		if ( $this->is_file( $source ) && $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
   340 			$this->delete( $source );
   353 			$this->delete( $source );
   341 
   354 
   342 			return true;
   355 			return true;
   343 		} else {
   356 		} else {
   344 			return false;
   357 			return false;
   397 	/**
   410 	/**
   398 	 * Checks if a file or directory exists.
   411 	 * Checks if a file or directory exists.
   399 	 *
   412 	 *
   400 	 * @since 2.5.0
   413 	 * @since 2.5.0
   401 	 *
   414 	 *
   402 	 * @param string $file Path to file or directory.
   415 	 * @param string $path Path to file or directory.
   403 	 * @return bool Whether $file exists or not.
   416 	 * @return bool Whether $path exists or not.
   404 	 */
   417 	 */
   405 	public function exists( $file ) {
   418 	public function exists( $path ) {
   406 		return @file_exists( $file );
   419 		return @file_exists( $path );
   407 	}
   420 	}
   408 
   421 
   409 	/**
   422 	/**
   410 	 * Checks if resource is a file.
   423 	 * Checks if resource is a file.
   411 	 *
   424 	 *
   445 	/**
   458 	/**
   446 	 * Checks if a file or directory is writable.
   459 	 * Checks if a file or directory is writable.
   447 	 *
   460 	 *
   448 	 * @since 2.5.0
   461 	 * @since 2.5.0
   449 	 *
   462 	 *
   450 	 * @param string $file Path to file or directory.
   463 	 * @param string $path Path to file or directory.
   451 	 * @return bool Whether $file is writable.
   464 	 * @return bool Whether $path is writable.
   452 	 */
   465 	 */
   453 	public function is_writable( $file ) {
   466 	public function is_writable( $path ) {
   454 		return @is_writable( $file );
   467 		return @is_writable( $path );
   455 	}
   468 	}
   456 
   469 
   457 	/**
   470 	/**
   458 	 * Gets the file's last access time.
   471 	 * Gets the file's last access time.
   459 	 *
   472 	 *
   582 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   595 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   583 	 *                               Default true.
   596 	 *                               Default true.
   584 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   597 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   585 	 *                               Default false.
   598 	 *                               Default false.
   586 	 * @return array|false {
   599 	 * @return array|false {
   587 	 *     Array of files. False if unable to list directory contents.
   600 	 *     Array of arrays containing file information. False if unable to list directory contents.
   588 	 *
   601 	 *
   589 	 *     @type string $name        Name of the file or directory.
   602 	 *     @type array ...$0 {
   590 	 *     @type string $perms       *nix representation of permissions.
   603 	 *         Array of file information. Note that some elements may not be available on all filesystems.
   591 	 *     @type string $permsn      Octal representation of permissions.
   604 	 *
   592 	 *     @type string $owner       Owner name or ID.
   605 	 *         @type string           $name        Name of the file or directory.
   593 	 *     @type int    $size        Size of file in bytes.
   606 	 *         @type string           $perms       *nix representation of permissions.
   594 	 *     @type int    $lastmodunix Last modified unix timestamp.
   607 	 *         @type string           $permsn      Octal representation of permissions.
   595 	 *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
   608 	 *         @type false            $number      File number. Always false in this context.
   596 	 *     @type int    $time        Last modified time.
   609 	 *         @type string|false     $owner       Owner name or ID, or false if not available.
   597 	 *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
   610 	 *         @type string|false     $group       File permissions group, or false if not available.
   598 	 *     @type mixed  $files       If a directory and `$recursive` is true, contains another array of files.
   611 	 *         @type int|string|false $size        Size of file in bytes. May be a numeric string.
       
   612 	 *                                             False if not available.
       
   613 	 *         @type int|string|false $lastmodunix Last modified unix timestamp. May be a numeric string.
       
   614 	 *                                             False if not available.
       
   615 	 *         @type string|false     $lastmod     Last modified month (3 letters) and day (without leading 0), or
       
   616 	 *                                             false if not available.
       
   617 	 *         @type string|false     $time        Last modified time, or false if not available.
       
   618 	 *         @type string           $type        Type of resource. 'f' for file, 'd' for directory, 'l' for link.
       
   619 	 *         @type array|false      $files       If a directory and `$recursive` is true, contains another array of
       
   620 	 *                                             files. False if unable to list directory contents.
       
   621 	 *     }
   599 	 * }
   622 	 * }
   600 	 */
   623 	 */
   601 	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
   624 	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
   602 		if ( $this->is_file( $path ) ) {
   625 		if ( $this->is_file( $path ) ) {
   603 			$limit_file = basename( $path );
   626 			$limit_file = basename( $path );
   614 
   637 
   615 		if ( ! $dir ) {
   638 		if ( ! $dir ) {
   616 			return false;
   639 			return false;
   617 		}
   640 		}
   618 
   641 
   619 		$ret = array();
   642 		$path = trailingslashit( $path );
       
   643 		$ret  = array();
   620 
   644 
   621 		while ( false !== ( $entry = $dir->read() ) ) {
   645 		while ( false !== ( $entry = $dir->read() ) ) {
   622 			$struc         = array();
   646 			$struc         = array();
   623 			$struc['name'] = $entry;
   647 			$struc['name'] = $entry;
   624 
   648 
   632 
   656 
   633 			if ( $limit_file && $struc['name'] !== $limit_file ) {
   657 			if ( $limit_file && $struc['name'] !== $limit_file ) {
   634 				continue;
   658 				continue;
   635 			}
   659 			}
   636 
   660 
   637 			$struc['perms']       = $this->gethchmod( $path . '/' . $entry );
   661 			$struc['perms']       = $this->gethchmod( $path . $entry );
   638 			$struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
   662 			$struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
   639 			$struc['number']      = false;
   663 			$struc['number']      = false;
   640 			$struc['owner']       = $this->owner( $path . '/' . $entry );
   664 			$struc['owner']       = $this->owner( $path . $entry );
   641 			$struc['group']       = $this->group( $path . '/' . $entry );
   665 			$struc['group']       = $this->group( $path . $entry );
   642 			$struc['size']        = $this->size( $path . '/' . $entry );
   666 			$struc['size']        = $this->size( $path . $entry );
   643 			$struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
   667 			$struc['lastmodunix'] = $this->mtime( $path . $entry );
   644 			$struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );
   668 			$struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );
   645 			$struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
   669 			$struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
   646 			$struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
   670 			$struc['type']        = $this->is_dir( $path . $entry ) ? 'd' : 'f';
   647 
   671 
   648 			if ( 'd' === $struc['type'] ) {
   672 			if ( 'd' === $struc['type'] ) {
   649 				if ( $recursive ) {
   673 				if ( $recursive ) {
   650 					$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
   674 					$struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive );
   651 				} else {
   675 				} else {
   652 					$struc['files'] = array();
   676 					$struc['files'] = array();
   653 				}
   677 				}
   654 			}
   678 			}
   655 
   679