Posts: 6
Name: Fiona
Location: rural australia
|
Do you use HTTP_Upload to validate file size? I am after some example code to see how I can validate the file size AND type using the PEAR package. I can see the code in PEAR's Upload.php file which checks and displays the error message for a large file size but I can't figure out how to utilise it, here is my code which checks for a valid file type successfully but fails to validate the size.
Code:
if (isset($_FILES['f'])) {
// Validate the type.
$allowedExtensions = array ('application/pdf', 'application/msword', 'application/rtf', 'text/plain');
if (in_array($_FILES['f']['type'], $allowedExtensions)) {
// Move the file over.
require_once('../Includes/Upload.php');
$upload = new http_upload('en');
$file = $upload->getFiles('f');
if (PEAR::isError($file)) {
die ($file->getMessage());
}
if ($file->isValid()) {
$file->setName('uniq');
$dest_dir = 'uploads';
$n = $file->getProp('size');
// validate the file size (THIS DOESN'T WORK)
if ($_FILES['f']['size'] > ($_POST['MAX_FILE_SIZE'])){
//$errors[] = 'File size exceeded. Must be less than 3mb.';
$echo = 'File size exceeded. Must be less than 3mb.';
}
There is a section built into PEAR which ckecks the file size and creates an error message, alas my skill set isn't up to transalting this code to utilize it, here it is...
|