Skip to content

Instantly share code, notes, and snippets.

@SebSept
Created December 11, 2015 12:01
Show Gist options
  • Save SebSept/880db864f2b1b20711d2 to your computer and use it in GitHub Desktop.
Save SebSept/880db864f2b1b20711d2 to your computer and use it in GitHub Desktop.
Virtual in memory php filesystem | https://github.com/mikey179/vfsStream example.
<?php
/**
* This is just a simple example how to use mikey179/vfsStream - https://github.com/mikey179/vfsStream
*
* Don't be fooled by the documentation.
* This lib is great but the documentation can let you thinks it's complicated or not 'natural' filesystem usage.
* This lib register a php stream so you can (almost) use filesystem function in a regular fashion.
*
* @see http://php.net/manual/en/book.stream.php
* @see http://php.net/manual/en/stream.streamwrapper.example-1.php
* @see https://github.com/mikey179/vfsStream
*
* Execution Will output :
* bool(true)
* bool(false)
* some data
*
*/
// installed using composer
require './vendor/autoload.php';
// initialisation
use org\bovigo\vfs\vfsStream,
org\bovigo\vfs\vfsStreamDirectory;
vfsStream::setup('/inadir/', 0777);
// done ! you can now use 'vfs://inadir' like a regular directory.
file_put_contents('vfs://inadir/yep', 'some data');
var_dump(file_exists('vfs://inadir/yep')); // true
var_dump(file_exists('vfs://inadir/nono')); // false
echo file_get_contents('vfs://inadir/yep'); // 'some data'
// make non writable dir
mkdir('vfs://inadir/nowrit');
chmod('vfs://inadir/nowrit', 0555);
if(@file_put_contents('vfs://inadir/nowrit/oops', 'mououhaha')) // false, dir not writable
{
throw new Exception("Oops dir was supposed not be writable.");
}
chmod('vfs://inadir/nowrit', 0700);
file_put_contents('vfs://inadir/nowrit/oki', 'mououhaha');
// @see https://github.com/mikey179/vfsStream/wiki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment