PHP Classes
elePHPant
Icontem

File: testFileDB.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of L  >  FileDB  >  testFileDB.php  >  Download  
File: testFileDB.php
Role: Example script
Content type: text/plain
Description: example
Class: FileDB
Stores (binary) files in a database
Author: By
Last change: changed to make it compliant to anyDB the db abstraction layer(http://www.phpclasses.org/anydb)
Date: 12 years ago
Size: 3,246 bytes
 

Contents

Class file image Download
<?php
////////////////////////////////////////////////////////////////////////
/*
    test page for FileDB.php
*/
////////////////////////////////////////////////////////////////////////

require "anyDB.php";
require
'FileDB.php';

////////////////////////////////////////////////////////////////////////

$host = 'localhost';
$database = 'ebm';
$user = '';
$password = '';
$db = anyDB::getLayer('MYSQL', '', '');

$filedb = new FileDB($db, $host, $database, $user, $password);

////////////////////////////////////////////////////////////////////////
// upload button clicked
if (@$HTTP_POST_VARS['action'] == 'upload') {

$file_data = $HTTP_POST_FILES['file'];
if (
is_uploaded_file($file_data['tmp_name'])) {
    
$file_path = dirname($file_data['tmp_name']) . '/' . $file_data['name'];
     if (
rename ($file_data['tmp_name'], $file_path)) {
?>
<html>
<body>
<?php

        
if ($filedb->add($file_path)) {
             echo
"Uploaded!<br>";
            
unlink($file_path);
         }
     }
}
    echo
"<a href=\"" . basename(__FILE__) . "\">back</a>\n";

////////////////////////////////////////////////////////////////////////
// clicked on a link
} else if (@$HTTP_GET_VARS['id']) {
       
$filedb->sendFile($HTTP_GET_VARS['id']);

////////////////////////////////////////////////////////////////////////
// display form
} else {
?>
<html>
<body>

<form enctype="multipart/form-data" action="<?=basename(__FILE__) ?>" method="post">
<input type="hidden" name="action" value="upload"></input>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"></input>
<input
type="file" name="file" maxlength="1000000" accept="text/*" ></input>
<input type="submit" value=" Upload file" ></input>
</form>

<?php
   
// display table
   
$res = $filedb->getWhere("id, file_name, file_size, descr, hits,
last_visited, last_edited, added"
, 'id', false);
    if (
is_array($res)) {

// display header
?>

<table border=1>
    <tr>
        <th>id</th>
        <th>filename</th>
        <th>size</th>
        <th>descr</th>
        <th>hits</th>
        <th>added</th>
        <th>last visited</th>
        <th>last edited</th>
        <th>show</th>
        <th>download</th>
    </tr>
<?php
       
// display file data
       
foreach($res as $data) {
?>
<tr>
            <td><?= $data['id'] ?></td>
            <td><?= $data['file_name'] ?></td>
            <td><?= $data['file_size'] ?></td>
            <td><?= ($data['descr'] ? $data['descr'] : '&nbsp;') ?></td>
            <td><?= $data['hits'] ?></td>
            <td><?= date("H:m:s \a\m d.m.y",
strtotime($data['added'])) ?></td>
            <td><?= date("H:m:s \a\m d.m.y",
strtotime($data['last_visited'])) ?></td>
            <td><?= date("H:m:s \a\m d.m.y",
strtotime($data['last_edited'])) ?></td>
            <td><a href="showFile.php?id=<?= $data['id']
?>">show</a></td>
            <td><a href="<?= basename(__FILE__) ?>?id=<?= $data['id']
?>">download</a></td>
    </tr>
<?php
       
}
?>
</table>
<?php
   
}
    echo
$filedb->_db->error;
}
////////////////////////////////////////////////////////////////////////
?>
</body>
</html>