SQLEFS Examples

<?php

require 'sqlefs/sqlefs.php'; # Loads class and assigns efs object.
$efs->connect("mysql:host=localhost;dbname=myefsdb username password") # connect to myefsdb database.
$efs->cd("c:/"); # select the C volume table and root path.

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
header('Content-Type: text/plain');
define('br', "\r\n");
define('hr', "===========================\r\n");

echo 'Creating file...' . br;
$efs->write('test.txt' , 'Some file contents.');
echo 'Copying file...' . br;
$efs->cp('test.txt', 'copy of test.txt');
echo 'Copying file again...' . br;
$efs->cp('test.txt', 'copy2 of test.txt');
echo hr;

echo 'Listing of: ' . $efs->cd() . br;
print_r(  $efs->ls()  );
echo hr;

echo 'Deleting test.txt...' . br;
$efs->rm('test.txt');
echo hr;

echo 'Listing of: ' . $efs->cd() . br;
print_r(  $efs->ls()  );
echo hr;

echo 'Deleting all files in folder...' . br;
$efs->rmdir();
echo hr;

echo 'Listing of: ' . $efs->cd() . br;
print_r(  $efs->ls()  );

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
define('br', '<br>');
define('hr', '<hr>');

echo 'Creating some files...' . br;
$efs->write('test1.txt' , 'Some file contents.');
$efs->write('test2.txt' , 'Some file contents.');
$efs->write('subdir1/testA.txt' , 'Some file contents.');
$efs->write('subdir2/testB.txt' , 'Some file contents.');
$efs->write('subdir2/deeper/testC.txt' , 'Some file contents.');
echo hr;

echo 'See files in ' . $efs->cd() . br;
print_r(  $efs->ls()  );
echo hr;

echo 'See subdirectories in ' . $efs->cd() . br;
print_r(  $efs->subs()  );
echo hr;

echo 'See files in subdir1/' . br;
print_r(  $efs->ls('subdir1/')  );
echo hr;

echo 'See details on files in subdir2/' . br;
print_r(  $efs->details('subdir2/')  );
echo hr;

echo 'See subdirectories in subdir2/' . br;
print_r(  $efs->subs('subdir2/')  );
echo hr;


# see more information using details 
echo 'Formatted file and directory listing...';
echo '<h3>Path: ' . $efs->cd() . '</h3>';
echo '<table border="1">';
$subs = $efs->subs();
foreach ($subs as $sd) {
	echo '<tr><td>' . $sd . '</td></tr>';
}
$files = $efs->details();
foreach ($files as $f) {
	echo '<tr><td>' . $f['filename'] . '</td>';
	echo '<td>' . $f['modified'] . '</td>';
	echo '<td>' . $f['owner'] . '</td>';
	echo '<td>' . $f['size'] . '</td></tr>';
}
echo '</table>';
echo '<hr>';


echo 'Deleting files...' . br;
$efs->rmdir();
$efs->rmdir('subdir1/');
$efs->rmdir('subdir2/');

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
header('Content-Type: text/plain');
define('br', "\r\n");
define('hr', "===========================\r\n");

echo 'Creating some files...' . br;
$efs->write('test1.txt' , 'Some file contents.');
$efs->write('test2.txt' , 'Some file contents.');
echo hr;

echo 'See details on files in current path...' . br;
print_r(  $efs->details()  );
echo hr;

# info is actually the same as details, but returns false if no file is found
echo 'See details on a specific file.' . br;
print_r(  $efs->info('test1.txt')  );
echo hr;

echo 'See the owner.' . br;
print_r(  $efs->owner('test1.txt')  );
echo hr;

echo 'See if a file exists.' . br;
print_r(  $efs->exists('test1.txt')  );
echo hr;

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php';
header('Content-Type: text/plain');

$data = array(
	'time' => $_SERVER['REQUEST_TIME'],
	'ip' => $_SERVER['REMOTE_ADDR'],
	'agent' => $_SERVER['HTTP_USER_AGENT'],
);

# save server data
$efs->save('test.dat' , $data);

# load and print request data
$loaded_data = $efs->load('test.dat');
print_r($loaded_data);

$efs->rm('test.dat');

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
header('Content-Type: text/plain');
define('br', "\r\n");
define('hr', "===========================\r\n");

$start_dir = $efs->cd();

# set path to some new place
echo 'I went to: ' .  $efs->cd('lets/visit/some/new/sub/directory/')  . br;
echo hr;

# demonstrate up
while ($start_dir != $efs->cd()) {
	echo 'up to ' .  $efs->up()  . br;
}
echo hr;

# now we're back to start, lets see subdirectories
echo 'Listing of ' . $efs->cd() . br;
print_r(  $efs->subs()  );
echo br;
echo 'Since we did not write a file there, that path is not listed as a subdirectory' . br ;
echo hr;

# listing a non-existing path
$new_path = date("Y/m/d/H/i/s");
echo 'Listing contents of ' . $new_path . br;
print_r(  $efs->ls($new_path)  );
echo br;
echo 'Listing contents of non-existing path does not cause an error.' . br;
echo hr;


?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
header('Content-Type: text/plain');
define('br', "\r\n");
define('hr', "===========================\r\n");

# The filepath specification
# [owner@][volume:][/][directory/][file.ext]

# change user to bob...
$efs->cd('bob@');

# change volume FYI...
# $efs->cd('disk2:');

# go to root
$efs->cd('/'); 

# go into a subdirectory
$efs->cd('subdir/');

# cd ignores the file part
$efs->cd('/subdir/file.txt');

# we can combine these...

# see Sams files
$efs->ls('sam@');

# see Sams files in a subdirectory
$efs->ls('sam@subdir/');

# use resolve to break paths apart
echo 'resolve sam@subdir/' . br;
print_r(  $efs->resolve('sam@subdir/')  );
echo hr;

echo 'resolve sam@subdir/ without current path context...' . br;
print_r(  $efs->resolve('sam@subdir/', array('','','','','',''))  );
echo hr;

# you can use the state array to retrieve current defaults...
echo 'Current defaults take from state array:' . br; 
echo 'Volume: ' . $efs->state['vol'] . br;
echo 'User: ' . $efs->state['user'] . br;
echo 'Path: ' . $efs->state['path'] . br;


# Note that some commands that request arguments that are specifically NOT paths
# such as disk and assign. 
# disk('C') versus cd('C:')
# assign('file', 'bob') although assign('bob@file') is ok.


?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 
header('Content-Type: text/plain');

$efs->write('test.txt' , 'Hello World');

echo $efs->read('test.txt');

?>
<?php

# BEFORE USING EXAMPLES YOU MUST HAVE THE FOLLOWING:
	# a database
	# A volume (aka SQLEFS table / disk)
	# Your SQL server username
	# Your SQL server password
	# A COPY of this file named sql_efs_login.php in the parent directory
	# That file modified to the connection parameters!!!

# MAKE SURE THIS PATH IS CORRECT...
require '../sqlefs.class.php';
$efs = new efsclass();

# SET TO YOUR OWN database, username, and password!!!
$efs->connect('mysql:mysql:host=localhost;dbname=myefsdb name pass');

# SET C to your own volume name if different than C
$efs->disk('c');

# EXAMPLES WILL CREATE, DELETE, AND MODIFY FILES WITHIN THIS FOLDER:
$efs->cd('/Test_Files/');

?>
<?php

# you must modify sqlefs_login.php for this example to work!
require_once '../sqlefs_login.php'; 

if (isset($_FILES['myupload'])) {
	# save the file
	$filename = $efs->upload('myupload');
	# download it...
	$efs->download($filename);

	# NOTE:
	# ordinarily we would sanitize a user given $filename with
	# $efs->tofile($filename)
	# but $efs->upload sanitizes to file name for us.

	# delete it in case its big
	$efs->rm($filename);
	# exit
	exit;
}
?>
<html>
<head><head>
<body>
<h1>Upload and Download Example</h1>
<form role="form" method="post" enctype="multipart/form-data">
<input type="file" name="myupload">
<input type="submit">
</form>
</body>
</html>