PHP Classes

File: myfat.php

Recommend this page to a friend!
  Classes of Mirko Mönninghoff   myFat   myfat.php   Download  
File: myfat.php
Role: Class source
Content type: text/plain
Description: The Class (Usage Information included)
Class: myFat
Store binaries in a MySQL based virtual hard drive
Author: By
Last change: filename updated
Date: 20 years ago
Size: 5,335 bytes
 

Contents

Class file image Download
<?php
/*
------------------------------------------------------------
Class: myFat

MySQL Database as Fileserver - Block Structure
2004 by Mirko Mönninghoff
mirko@mouseattack.de

Version 0.2 alpha / !working!
Date: 04/17/2004
Last update: 04/18/2004

The class stores binary data - any kind of files in a mysql
database. It creates a virtual FAT and handles the data
like a simple filesystem. Perfect for people with less
webspace and one mysql database ;-)

------------------------------------------------------------
IF YOU DAMAGE SOMETHING WITH THIS CLASS, ITS NOT MY FAULT !!
YOU HAVE BEEN WARNED -NO WARRENTY- TEST IST AT YOUR OWN RISK
------------------------------------------------------------

If you like/use the script: Drop me some lines per mail !
Comments (positive) are welcome to !

Don't remove this header information !
------------------------------------------------------------

Installation:

Create a mysql database or decide for an already in use db.
Fill in the connection details ind the class header (see
below)

The database needs 2 tables:

SQL:

CREATE TABLE `myfat_cluster` (
  `block_position` int(10) unsigned default '0',
  `file_uid` int(10) unsigned default '0',
  `block_data` longblob
) TYPE=MyISAM;

CREATE TABLE `myfat_fat` (
  `file_name` varchar(255) NOT NULL default '',
  `file_uid` int(11) NOT NULL auto_increment,
  PRIMARY KEY (`file_uid`)
) TYPE=MyISAM AUTO_INCREMENT=1000 ;


For large files its good to adjust some php.ini settings:
-----------------------------------------------------------
my settings (in test envirement):

php.ini:

[...]
max_execution_time = 240
max_input_time = 240
memory_limit = 64M
post_max_size = 32M
file_uploads = On # this must be !
upload_max_filesize = 24M
[...]

------------------------------------------------------------
ToDo: (If i find some time)
Fields for more Fileinformation(Size,Blocks,Type,etc),
Folders,
------------------------------------------------------------
   

*/

class myFat {

    var
$DB_SERVER = "192.168.0.88"; # mysql server adress *adjust to your needs*
   
var $DB_USER = "myfat"; # database username *adjust to your needs*
   
var $DB_PASS = "password"; # database user password *adjust to your needs*
   
var $DB_NAME = "myfat"; # database name *adjust to your needs*
   
var $TABLE_FAT = "myfat_fat"; # the table for the vfat
   
var $TABLE_BLOCKS = "myfat_cluster"; # this table takes the data blocks
   
var $myScriptName = "index.php"; # the target page for all links - in this case the self page
   
var $DB_LINK = NULL;
    var
$BLOCKSIZE = 16384; # store 16K blocks
   
   
function myFat(){
       
/* Constructor */
       
       
       
$this->DB_LINK = @mysql_connect($this->DB_SERVER,$this->DB_USER,$this->DB_PASS) or die("SQL: Connect Error");
            @
mysql_select_db($this->DB_NAME) or die ("SQL: Database not found");
           
            if (isset(
$_GET["load"])) $this->download($_GET["load"]);
        if (isset(
$_GET["upload"])) $this->upload();
        if (isset(
$_GET["delete"])) $this->delete($_GET["delete"]);
   
    }


    function
listfiles(){
   
   
       
$sql = "select file_name,file_uid from $this->TABLE_FAT order by file_name";
       
$res = mysql_query($sql);
       
        echo
"<table cellpadding=4>";
        echo
"<tr><td>";
        echo
"Directory";
        echo
"</td><td>";
        echo
"&nbsp;";
        echo
"</td></tr>";

        while (
$row = mysql_fetch_array($res)){
            echo
"<tr><td>";
            echo
"<a title='Click to download' href='$this->myScriptName?load=$row[file_uid]'>$row[file_name]</a><br>";
            echo
"</td><td>";
            echo
"<a title='Click to delete' href='$this->myScriptName?delete=$row[file_uid]'>[Delete]</a>";
            echo
"</td></tr>";
        }
        echo
"</table>";

    }
   
    function
upload(){

       
$file = $GLOBALS["filename"];
       
$file_name = $GLOBALS["filename_name"];
       
        if (!
file_exists($file)) return 0;
       
       
$this->save($file,$file_name);
    }
   
    function
save($file,$file_name){

       
$file_size = filesize($file);
       
$block_counter = 0;
       
       
$sql = "insert into $this->TABLE_FAT (file_name) values ('$file_name')";
       
mysql_query ($sql);
       
       
$file_uid = mysql_insert_id($this->DB_LINK);
       
       
$handle = fopen ($file, "r");
        while (!
feof($handle)) {
           
$buffer = addslashes(fread($handle, $this->BLOCKSIZE));
           
$sql = "insert into $this->TABLE_BLOCKS (block_position,file_uid,block_data) values (".$block_counter++.",".$file_uid.",'$buffer')";
           
mysql_query ($sql);
        }
       
fclose ($handle);
    }
   
   
    function
delete($file_uid){
       
$sql = "delete from $this->TABLE_BLOCKS where file_uid='$file_uid'";
       
mysql_query($sql);
       
$sql = "delete from $this->TABLE_FAT where file_uid='$file_uid'";
       
mysql_query($sql);
    }
   
    function
download($file_uid){

       
$sql = "select file_name from $this->TABLE_FAT where file_uid='$file_uid'";
       
$res = mysql_query($sql);
       
$row = mysql_fetch_array($res);
       
$file_name = urlencode($row["file_name"]);

       
$sql = "select * from $this->TABLE_BLOCKS where file_uid='$file_uid' order by block_position";
       
$res = mysql_query($sql);
       
       
header("Content-Type: x-type/subtype");
       
//header("Content-Length: ");
       
header("Content-Disposition: attachment; filename=$file_name");
               
        while (
$row = mysql_fetch_array($res)){
            echo
$row["block_data"];
        }
       
        exit;
    }
}

?>