Hi all
I have some functionality that allows the upload of files such as MS word docs, zips etc. It works fine on my pc and another test machine but doesn't seem to work on two other machines I've tested. All machines have Windows XP installed. The first machine that is having problems only has issues uploading PDF's. The second machine having problems can't upload anything at all.
Please could someone have a look at my code below and tell me if there's anything there that could be causing these problems. I'm pretty sure I have the MIME types right. I'm really puzzled as to what is causing this.
P.S. My code isn't the best in the world. Note that this upload script is not being used publicly on the website so security issues aren't top priority.
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>.";
}
}
}