Monday, May 30, 2005

Using Gadget_Upload

The GWDK comes with a handy -KISS- upload handler.


Using it is as simple as:



<?php

define
('GADGET_CHARSET', 'ISO-8859-1');

require_once
'PEAR.php';
require_once
'Gadget/Upload.php';

if (
$pdf = &Gadget_Upload::getFile('uploads_form_name')) {
// use M or K as shorthand
if ($pdf->checkSize('2M')) {
// check content type
if ($pdf->checkType('application/pdf')) {
$pdf->nameToSafe();
// the only method that returns PEAR_Error
$error = $pdf->moveTo('../uploads');
}
}
}
?>

Just squeeze that into a while loop for multiple uploads:



<?php

while ($img = &Gadget_Upload::getFile('images')) {
if (
$img->checkSize('50k')) {
// check for general "image" content type
if ($img->checkType('image')) {
// pass prefix for uniqid() as parameter
$img->nameToUniq('img');
// the only method that returns PEAR_Error
$error = $img->moveTo('./images');
}
}
}
?>

Have fun...

6 comments: