exists( $filepath ) || $append ) { $result = $wp_filesystem->put_contents( $filepath, $content ); } elseif ( $append ) { $existing_content = $wp_filesystem->get_contents( $filepath ); $result = $wp_filesystem->put_contents( $filepath, $existing_content . $content ); } if ( false === $result ) { self::$last_error = 'Trying to write to the file failed'; } return (bool) $result; } /** * Getter for the last error variable of the class * * @return string * * @since 4.4.3 */ public static function get_last_error(): string { return self::$last_error; } /** * Reads entire file into memory and returns the content as a string. * IMPORTANT: Don't use that method if you are expecting large files. * * @param string $filename - The full name of the file (including the path). * * @return string * * @since 4.4.3.2 */ public static function read_entire_content_memory( string $filename ): string { global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); if ( $wp_filesystem->exists( $filename ) ) { return $wp_filesystem->get_contents( $filename ); } return ''; } /** * Returns the file size in human readable format. * * @param string $filename - The name of the file (including path) to check the size of. * * @return string * * @since 5.0.0 */ public static function format_file_size( string $filename ): string { global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); if ( $wp_filesystem->exists( $filename ) ) { $size = filesize( $filename ); $units = array( 'B', 'KB', 'MB', 'GB', 'TB' ); $formatted_size = $size; $units_length = count( $units ) - 1; for ( $i = 0; $size >= 1024 && $i < $units_length; $i++ ) { $size /= 1024; $formatted_size = round( $size, 2 ); } return $formatted_size . ' ' . $units[ $i ]; } return '0KB'; } } }