Im re-rolling a validation class built for checking login/register forms to be used in place of a paper upload form. I copied the syntax, and re-created a new function explicitly for checking papers. My problem now is that its not working, and there are no clear signs as to why. Im checking PHP error.log and its not showing anything out of the ordinary.
Heres my upload.php with the form, and PHP code:
<?php
require 'core/init.php';
$user = new User();
$user->protect();
$_error = false;
$_papererror = false;
if (Input::exists()) {
if (Token::check(Input::get('token'))) {
//print_r($_FILES['paper']);
$validate = new Validate();
$papervalidate = new Validate();
$validation = $validate->check($_POST, array(
'papername' => array('required' => true, 'max' => 50)
));
$papervalidation = $papervalidate->checkPaper($_FILES, array(
'paper' => array('required' => true, 'type' => Input::get('paper')['type'])
));
if($validation->passed() && $papervalidation->passed()) {
// handle upload, and then redirection
echo "Success";
} else {
$_error = true;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hutcheson Website</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<!--[if lt IE 9]>
<script src="js/html5shiv.min.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php include 'includes/_navbar.php'; ?>
<?php
if($_error === true || $_papererror == true) {
?>
<div class="container-fluid">
<div class="row">
<div class=" col-lg-4 col-lg-offset-4 col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
<div class="alert alert-danger">
<ul style="list-style: none;">
<?php
foreach($validation->errors() as $error) {
echo "<li><strong>ERROR!!</strong> " . $error . "</li>";
}
foreach($papervalidation->errors() as $error) {
echo "<li><strong>ERROR!!</strong> " . $error . "</li>";
}
?>
</ul>
</div>
</div>
</div>
</div>
<?php
}
?>
<div class="container-fluid">
<div class="col-lg-6 col-lg-offset-3 col-md-8 col-md-offset-2">
<form action="" method="post" class="form-horizontal validate" enctype="multipart/form-data">
<fieldset>
<legend>Upload a Paper</legend>
<div class="form-group">
<label for="papername" class="col-md-2 control-label">Paper Name</label>
<div class="col-md-10">
<input type="text" class="form-control" id="papername" name="papername" placeholder="Give me a fancy name" />
</div>
</div>
<div class="form-group">
<label for="format" class="col-md-2 control-label">Formatting</label>
<div class="col-md-10">
<select name="format" id="format" class="form-control">
<option value="0" selected>MLA</option>
<option value="1">APA</option>
</select>
</div>
</div>
<div class="form-group">
<label for="paper" class="col-md-2 control-label">Paper</label>
<div class="col-md-10">
<input type="file" class="form-control" id="paper" name="paper" />
<span class="help-block">File formats accepted are .doc .docx .txt .rtf</span>
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-xs-offset-2">
<button type="submit" class="btn btn-primary col-xs-12">Submit</button>
</div>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</fieldset>
</form>
</div>
</div>
</body>
</html>
Here is the Validate.php class where Im calling to validate the form:
<?php
/**
* Created by PhpStorm.
* User: jonathanstowe
* Date: 12/12/13
* Time: 1:43 AM
*/
class Validate {
private $_passed = false;
private $_errors = array();
private $_db = null;
private $_fields = array(
'username' => 'Username',
'password' => 'Password',
're_password' => 'Repeat Password',
'name' => 'Name',
'cur_pass' => 'Current Password',
'new_pass' => 'New Password',
're_pass' => 'Repeat Password',
'papername' => 'Paper name',
'paper' => 'A paper',
'format' => 'Format is required'
);
public function __construct() {
$this->_db = DB::getInstance();
}
/*
*
*/
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
$item = escape($item);
if($rule === 'required' && empty($value)) {
$this->addError("{$this->refactor($item)} is required");
} else if(!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$this->refactor($item)} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$this->refactor($item)} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$this->refactor($rule_value)} must match {$this->refactor($item)}.");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($check->count()) {
$this->addError("{$this->refactor($item)} already exists.");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
public function checkPaper($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = $source['paper'];
$item = escape($item);
//die($source['paper']);
if($rule === 'required' && empty($value['paper'])) {
$this->addError("{$this->refactor($item)} is required");
} else if(!empty($value)) {
switch($rule) {
case 'type':
$_types = array(
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain',
'text/rtf',
'application/msword'
);
if(!in_array($value, $_types)) {
$this->addError("{$this->refactor($item)} must be in a .doc .docx .txt .rtf .pdf file format");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
/*
*
*/
private function addError($error) {
$this->_errors[] = $error;
}
/*
*
*/
public function errors() {
return $this->_errors;
}
/*
*
*/
public function passed() {
return $this->_passed;
}
/*
*
*/
public function refactor($item) {
$field = array('username', 'password', 're_password', 'name', 'cur_pass', 'new_pass', 'rep_pass');
$item = strtolower($item);
if(in_array($item, $field) === true) {
return $this->_fields[$item];
}
return $this->_fields[$item];
}
}
And using Console.app on my mac viewing the apache2 error.log shows no errors when trying to upload a pair of any format.
Ive tried changing the $_FILES path to be less dynamic, and more static since this is the only upload form that needs to be validated.