class DocumentUpload
{
//Variables values gained from the __construct() method
public $inputName;
public $original;
public $timeOriginal;
public $tempName;
public $docType;
public $folder;
public $dbFolder;
public $page;
public $pathAndFile;
public $pathTimeFile;
public $dbFile;
public $dbTimeFile;
//Variables used in the upload() method
public $thumb;
public $success;
public $result;
public $mailSubject = "";
//Variable used to see which path and filename must be added to the database
public $timedFile;
//Array of acceptable MIME types
public $permitted = array('application/pdf', 'application/zip', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel');
//variables set assuming the file is unacceptable
public $sizeOK = false;
public $typeOK = false;
public function __construct($inputName, $folder, $dbFolder, $page)
{
$this->inputName = $inputName;
$this->original = str_replace(array(" ", 'PDF'), array("_", 'pdf'), $_FILES[''.$inputName.'']['name']);
$this->timeOriginal = time().$this->original;
$this->tempName = $_FILES[''.$inputName.'']['tmp_name'];
$this->docType = $_FILES[''.$inputName.'']['type'];
$this->folder = $folder;
$this->dbFolder = $dbFolder;
$this->pathAndFile = $folder.$this->original;
$this->pathTimeFile = $folder.time().$this->original;
$this->dbFile = $dbFolder.$this->original;
$this->dbTimeFile = $dbFolder.time().$this->original;
$this->page = $page;
}
//Upload an image to the server
public function upload($chmod)
{
//check that the file is of a permitted MIME type
foreach($this->permitted as $type)
{
if($type == $this->docType)
{
$this->typeOK = true;
break;
}
}
//Check if the file type has been verified as valid
if($this->typeOK)
{
switch($_FILES[''.$this->inputName.'']['error'])
{
case 0:
// Make sure file of the same name does not exist
if (!file_exists($this->pathAndFile))
{
$this->success = move_uploaded_file($this->tempName, $this->pathAndFile);
chmod($this->pathAndFile, $chmod);
//Variable used to see which path and filename must be added to the database
$this->timedFile = false;
}
else
{
$this->success = move_uploaded_file($this->tempName, $this->pathTimeFile);
chmod($this->pathTimeFile, $chmod);
//Variable used to see which path and filename must be added to the database
$this->timedFile = true;
}
//If the file was uploaded
if ($this->success)
{
$this->result = "Document added successfully.";
}
else
{
$this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
}
break;
case 3:
$this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
default:
$this->result = "System error uploading file. Contact the <a class=\"clrBrown\" href=\"mailto:test@website.com".$this->mailSubject."\">webmaster</a>";
}
}
else if($_FILES[''.$this->inputName.'']['error'] == 4)
{
$this->result = 'No file selected';
}
else
{
$this->result = "File cannot be uploaded. Acceptable file types: ppt, xls, doc, pdf, zip. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
}
}
}