# sqlite-object-cache/1.5.4/includes/lib/class-file.php

SQLite Object Cache, version 1.5.4. 75 lines.

- Page: https://pluginprobe.com/plugins/sqlite-object-cache/1.5.4/code/includes/lib/class-file.php
- Raw: https://pluginprobe.com/plugins/sqlite-object-cache/1.5.4/raw/includes/lib/class-file.php
- Modified: 2025-02-25T14:22:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sqlite-object-cache/1.5.4/code/includes/lib/class-file.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}
/**
 * Read and write an existing file.
 */
class SQLite_Object_Cache_File {

  /**
   * Read data from file.
   *
   * @param string $filename The pathname of the file to read.
   *
   * @return bool|string The file's contents. Empty string if the file doesn't already exist. false if it is not readable.
   */
  public static function read( $filename ) {
    if ( ! file_exists( $filename ) ) {
      return '';
    }

    if ( ! is_readable( $filename ) ) {
      return false;
    }

    $content = file_get_contents( $filename );
    return self::remove_zero_space( $content );
  }

  /**
   * Save data to file.
   *
   * @param string $filename
   * @param string $data
   *
   * @return bool True if the save succeeded.
   */
  public static function save( $filename, $data ) {

    if ( ! file_exists( $filename ) ) {
      return false;
    }
    $data = self::remove_zero_space( $data );
    $ret  = file_put_contents( $filename, $data, LOCK_EX );

    return ( false !== $ret );
  }

  /**
   * Remove Unicode zero-width spaced <200b><200c> and BOMs
   *
   * @param array|string $content
   *
   * @return array|string|string[]
   */
  public static function remove_zero_space( $content ) {
    if ( is_array( $content ) ) {
      $content = array_map( __CLASS__ . '::remove_zero_space', $content );
      return $content;
    }

    // Remove UTF-8 BOM if present
    if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) {
      $content = substr( $content, 3 );
    }

    $content = str_replace( "\xe2\x80\x8b", '', $content );
    $content = str_replace( "\xe2\x80\x8c", '', $content );
    $content = str_replace( "\xe2\x80\x8d", '', $content );

    return $content;
  }

}

```
