Erik's blog

Code, notes, recipes, general musings

Archive for October 2008

PHP atomic write function

with 4 comments

Motivation:

An atomic write function for those who appreciate the convenience of file_put_contents(), but need an atomic operation.  Please leave a comment if you have a more convenient and/or efficient solution.

Update (5/31/11):

Now using flock, as recommended by Petr and Hayden in their comments below, to avoid race condition. Thanks for the help!

Source code:


function atomic_put_contents($filename, $data)
{
    // Copied largely from http://php.net/manual/en/function.flock.php
    $fp = fopen($filename, "w+");
    if (flock($fp, LOCK_EX)) {
        fwrite($fp, $data);
        flock($fp, LOCK_UN);
    }
    fclose($fp);
}

Example usage:


$content = 'some content';

atomic_put_contents('path/file.ext', $content);

//creates file called path/file.ext containing 'some content'

Advertisements

Written by Erik

October 26, 2008 at 1:58 am

Posted in code

Tagged with

PHP class to manage read-only vars and arrays

leave a comment »

Source code:


class Constants
{
    private $var_cache;

    public function set($key, $val)
    {
        if(isset($this->var_cache[$key]))
        {
            throw(new Exception("Constants->get() error: var $key has already been defined"));
        }
        else
        {
            $this->var_cache[$key] = $val;
        }
    }

    public function get($key)
    {
        return $this->var_cache[$key];
    }
}

Example usage:

$constants = new Constants();

$constants->set('base_url', 'www.domain.com/path');

...

$img_src_url = $constants->get('base_url').'/img/file.jpg';

echo "<img src='$img_src_url'/>";

Written by Erik

October 26, 2008 at 12:04 am

Posted in code

Tagged with

Everything

leave a comment »

Must everything be important?

My favorite color is

the golden green produced by

sunlight shining through a leaf,

particularly those of fig trees and grape vines.

Sitting in the shade of a grape arbor,

I am awash in my favorite tone.

It’s calming;

my eyes are soothed.

I feel supported by the clay and dried grass beneath me.

I can imagine the water flowing

through the roots of this plant,

to transpire from its leaves.

I am home.

My favorite color is important to me.

Written by Erik

October 25, 2008 at 10:57 pm

Posted in poetry

Tagged with

sap of life

leave a comment »

the sap of life

flows

from fingers to toes

and yet

is also still

like the air

we breath it in

and move in it

it is made of us

and we of it

Written by Erik

October 25, 2008 at 10:42 pm

Posted in poetry

Tagged with

a rock ledge

leave a comment »

a rock ledge

far from the sounds of leaf blowers, garbage trucks, and trains

close to the nature of things

insignificant

below the snow

above a river wrapped in green

rust-colored

ancient

exposed to air and unabated starlight

Written by Erik

October 25, 2008 at 10:39 pm

Posted in poetry

Tagged with

Mother & baby by Os Gemeos

leave a comment »



Mother & baby by Os Gemeos, originally uploaded by server pics.

Amazing art by Os Gemeos

Written by Erik

October 23, 2008 at 12:23 am

Posted in Uncategorized

Beautiful graffiti by Daim

leave a comment »



Daim, originally uploaded by trashisfesch.

Written by Erik

October 23, 2008 at 12:22 am

Posted in Uncategorized