PHP Classes

File: testFileDB2.php

Recommend this page to a friend!
  Classes of L   FileDB   testFileDB2.php   Download  
File: testFileDB2.php
Role: Example script
Content type: text/plain
Description: example using Uploader.php
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: 21 years ago
Size: 3,313 bytes
 

Contents

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

    Shows how the upload and the db class can easily be used together.
*/
////////////////////////////////////////////////////////////////////////

require "../anyDB/anyDB.php";
// http://www.phpclasses.org/browse.html/package/846.html
require "../uploader/Uploader.php";
// http://www.phpclasses.org/browse.html/package/949.html
require 'FileDB.php';

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

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

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

////////////////////////////////////////////////////////////////////////
// upload button clicked

if ($up->wasSubmitted()) {
   
$overwrite = true;
   
$allowedTypes = array("image/bmp","image/gif","image/pjpeg","image/jpeg","image/x-png");
   
$array = $up->uploadTo('./', $overwrite, $allowedTypes);
    if (
is_array($array)) {
        foreach(
$array as $file) {
             if (
$filedb->add($file['full_path'])) {
                 echo
$file['name'] . " was uploaded!<br>";
                
unlink($file['full_path']);
             }
        }
    }
    echo
$up->error;
    echo
"<p><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>

<?php
   
echo $up->openForm(basename(__FILE__));
    echo
$up->fileField();
    echo
$up->closeForm();
   
// 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>