public function validate($validator) { if (!isset($_FILES[$this->name])) { return true; } $tmpFile = $_FILES[$this->name]; $valid = $this->upload->validate($tmpFile); if (!$valid) { $errors = $this->upload->getErrors(); if ($errors) { foreach ($errors as $error) { $validator->validationError($this->name, $error, "validation"); } } return false; } return true; }
public function testUploadDeniesNoExtensionFilesIfNoEmptyStringSetForValidatorExtensions() { // create tmp file $tmpFileName = 'UploadTest-testUpload'; $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $tmpFileContent = ''; for ($i = 0; $i < 10000; $i++) { $tmpFileContent .= '0'; } file_put_contents($tmpFilePath, $tmpFileContent); // emulates the $_FILES array $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => '', 'error' => UPLOAD_ERR_OK); $v = new UploadTest_Validator(); $v->setAllowedExtensions(array('txt')); // test upload into default folder $u = new Upload(); $result = $u->loadIntoFile($tmpFile); $this->assertFalse($result, 'Load failed because extension was not accepted'); $this->assertEquals(1, count($u->getErrors()), 'There is a single error of the file extension'); }