PHP Classes

File: up.php

Recommend this page to a friend!
  Classes of Mike Gifford   XUpload/ViewDB   up.php   Download  
File: up.php
Role: Example script
Content type: text/plain
Description: The main script which interacts with the classes
Class: XUpload/ViewDB
Upload files and view directory contents
Author: By
Last change:
Date: 22 years ago
Size: 6,791 bytes
 

Contents

Class file image Download
<?php
#######################################
# Author: Mike Gifford #
# Organization: OpenConcept Consulting #
# e-mail: mike@openconcept.ca #
# See related classes for more credits #
#######################################

// Configuration
   
$uploadPath = '/usr/local/localhost/XUploadDB/uploads'; // --> / (NO FINAL SLASH)
   
$uploadURL = 'http://localhost/XUploadDB/uploads'; // --> http:// (NO FINAL SLASH)
   
$classPath = '/usr/local/localhost/XUploadDB/class';
   
$acceptedExtensions = array(".doc", ".xls", ".txt", ".pdf", ".gif", ".jpg", ".gz", ".zip", ".rar", ".ppt", ".mp3", ".html", ".htm");
   
$displayList = 'files'; // could be "folder" "files" "all"
   
$phpself = $HTTP_SERVER_VARS['PHP_SELF'];
   
$version = split ("\.", phpversion());
   
   
$databaseEnabled = '1'; // 1 if enbaled
   
if ($databaseEnabled) {
       
$_DB['DB_Host'] = "localhost";
       
$_DB['DB_Database'] = "be5";
       
$_DB['DB_User'] = "user";
       
$_DB['DB_Password'] = "password";
       
        require(
"$classPath/XUploadDB.class");
        require(
"$classPath/ViewDirDB.class");
       
$viewDirectoryOBJ = new ViewDirDB;
    } else {
        require(
"$classPath/XUpload.class");
        require(
"$classPath/ViewDir.class");
       
$viewDirectoryOBJ = new myDIR;
    }

    require(
"$classPath/XUploadForm.class");
   
$uploadFormOBJ = new XUploadForm("$phpself");
   

// Header
   
$content = "<html>\n<body>";

// Work around for register_globals = Off
if ( $version < 4.1 ) {
   
$delete = $_GET['delete'];
   
$orderby = $_GET['orderby'];
} else {
   
$delete = $HTTP_GET_VARS['delete'];
   
$orderby = $HTTP_GET_VARS['orderby'];
}

// Delete Uploaded files
if (!empty($delete)) {
    if (
$databaseEnabled)
       
$uploadFileOBJ = new XuploadDB("file1", '', '', $acceptedExtensions);
    else
       
$uploadFileOBJ = new Xupload("file1", '', '', $acceptedExtensions);
   
$uploadFileOBJ->setDir($uploadPath);
     if(
$uploadFileOBJ->removeFile("$delete")) {
         if (
$databaseEnabled)
            
$uploadFileOBJ->deleteDB($delete);
       
$content = "Successfully deleted $delete";
    }
}

// ===================
// Save Uploaded files
// ===================

if (isset($HTTP_POST_VARS['submit'])) {
 
// Create New Objects
   
if ($databaseEnabled)
       
$uploadFileOBJ = new XuploadDB("file1", '', '', $acceptedExtensions);
    else
       
$uploadFileOBJ = new Xupload("file1", '', '', $acceptedExtensions);
   
$uploadFileOBJ->setDir( "$uploadPath" );

   
// if you want to remove "oldfile.pdf" if exists in upload dir and perms are ok
    // you must use setDir() method previously
    //$uploadFileOBJ->removeFile("oldfile.pdf");

   
$uploadFileOBJ->xCopy();

   
// if you want to rename the remote file
    // yo must set it in the object instance, set to 1 to rename file, default value is 0
    // $myU3->xCopy(); //random name keeping extension
    // $myU3->xCopy("thenewname.ext") // you must provide the complete file name
    // $myU3->xCopy($my_new_name_var) // providing a var that contains the new name

   
$content .= $uploadFileOBJ->show_progressMessage();
   
    if (
$uploadSuccessful AND $databaseEnabled) {
       
$fileType = $viewDirectoryOBJ->get_type($filename);
       
$uploadFileOBJ->newDB($filename, $uploadPath, $uploadURL, $shortDescription, $longDescription, $fileType, $uploadedBy, $imageHeight, $imageWidth);
    }
}

// ===================
// Display Uploaded files
// ===================

$viewDirectoryOBJ->setFIND("$displayList");
$viewDirectoryOBJ->setMASK($acceptedExtensions); // ("*.html,*.htm,*.txt") separated with comma
$viewDirectoryOBJ->setROOT("$uploadPath");
$RESULT = $viewDirectoryOBJ->getRESULT();
// print_r($RESULT);
// Ordering Results
foreach ($RESULT as $val) {
   
$sortarray[] = $val["$orderby"];
}
// if (!isset($logic) || empty($logic)) $logic = "asc"; $sort = 'SORT_ASC';
// elseif ($logic == "desc") $logic = "asc"; $sort = 'SORT_ASC';
// elseif ($logic == "asc") $logic = "desc"; $sort = 'SORT_DESC';
array_multisort($sortarray, $RESULT);

$content .= "<table cellpadding='5'>\n";
$content .= "<tr><td colspan='6' align=center>Files Found ".count($RESULT)."</td></tr>\n";
 
$content .= "<tr>";
$content .= "<td>&nbsp;<strong>Thumbnail</strong></td>";
// $content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=folder\">Folder</a></strong></td>";
$content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=file\">File</a></strong></td>";
$content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=rawSize\">Size</a></strong></td>";
$content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=type\">Type</a></strong></td>";
$content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=time\">Time</a></strong></td>";
$content .= "<td>&nbsp;<strong><a href=\"$phpself?orderby=perms\">Permission</a></strong></td>";
$content .= "<td>&nbsp;<strong>Delete</strong></td>";
$content .= "</tr>\n";

 for (
$i=0 ; $i < count($RESULT); $i++ )
 {
   
$bgcolor = ( $i%2 == 0 ) ? "#DDDDDD" : "#EFEFEF";
   
    if (
$RESULT[$i]["type"]=='image') {
       
$ext = strrchr($RESULT[$i]["file"] , ".");
        if((!
strcasecmp ($ext, ".jpg")) || (!strcasecmp ($ext, ".jpeg"))) {
           
$uploadID = $viewDirectoryOBJ->findUploadID($RESULT[$i]["file"]);
            
$thumbNailImages = "<img src=\"image.php?ID=$uploadID\">";
         }
     }
 
    
$content .= "<tr bgcolor='$bgcolor'>";
    
$content .= "<td>$thumbNailImages</td>";
    
// $content .= "<td>&nbsp;".$RESULT[$i]["folder"]."</td>";
    
$content .= "<td>&nbsp;<a href=\"$uploadURL/".$RESULT[$i]["file"]."\"target=\"_blank\">".$RESULT[$i]["file"]."</a></td>";
    
$content .= "<td>&nbsp;".$RESULT[$i]["size"]."</td>";
    
$totalFileSize += $RESULT[$i]["rawSize"];
    
$content .= "<td>&nbsp;".$RESULT[$i]["type"]."</td>";
     if (
$RESULT[$i]["time"])
        
$content .= "<td>".strftime("%d.%m.%y - %H:%M",$RESULT[$i]["time"])."</td>";
     else
        
$content .= "<td>&nbsp;</td>";
    
$content .= "<td>&nbsp;".$RESULT[$i]["perm"]."</td>";
    
$content .= "<td>&nbsp;<a href=\"$phpself?delete=".$RESULT[$i]["file"]."\">del</a></td>";
    
$content .= "</tr>\n";
 }

 
$content .= "</table>\n";
 
 
$content .= "<strong>Total Uploads: " . $viewDirectoryOBJ->humanFileSize("$totalFileSize") . "</strong>";
 
// ===================
// Show Form
// ===================
 
$content .= $uploadFormOBJ->begin();
$content .= $uploadFormOBJ->setFormMaxFileSize("2097152");
 
$content .= $uploadFormOBJ->setFormFileInput("file1");
// $content .= $uploadFormOBJ->setFormFileInput("file2");
// $content .= $uploadFormOBJ->setFormFileInput("file3");
 
$content .= $uploadFormOBJ->setFormButton("submit","Upload");
$content .= $uploadFormOBJ->end();

$content .= "</body>\n</html>";

echo
$content;

?>