Login   Register  
PHP Classes

File: v1/README.md

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of jstar88  >  Safe IO  >  v1/README.md  >  Download  
File: v1/README.md
Role: Documentation
Content type: text/plain
Description: Documentation
Class: Safe IO
Access files using locks to prevent corruption
Author: By
Last change:
Date: 1 year ago
Size: 1,938 bytes


Class file image Download

A class usefull to avoid concurrancy managing files.

## Introduction
SafeIO is an utility to open - write contents in files based on PHP *flock* function.
As PHP manual say, multithreading is not supported yet.

## Installation

Download *SafeIO.php* and include it in your scripts:


## Basic usage

Retrive data from a file:

    $filePath = "data.txt";

Exporting data in a file:

    $filePath = "data.txt";
    $contents = "some cool data";
    $reset = true; // true means discard old content inside the file
    SafeIO::save($filePath, $contents , $reset);

## More for you: transactions

Transactions are usefull to ensure an atomic behavior. Expecially, in generic applications
, you need to execute actions that require time (like DB queries,loop etc) to know exactly what you need to do:
in this situation a common problem is to keep unchanged the target file from modification of others processes.
It's easier to see the code ;)

Starting a transaction:

    $filePath = "data.txt";

Stopping a transaction and writing the file:

    $filePath = "data.txt";
    $contents = "some cool data";
    $reset = true;
    SafeIO::stopTransaction($filePath, $contents, $reset );
* calling function *SafeIO::save* will automatically end and flush the active transaction.
* you can keep active multiple transactions(one per file)

An example:
    SafeIO::startTransaction("data.txt","hello world!");
    //  ... Other users can't write data.txt while you are inside here ...
    SafeIO::stopTransaction("data.txt"); // now the lock is released   
    echo SafeIO::open("data.txt");