Sorry to wake up an old thread, but I was actually looking for something exactly like this, but I couldn't find anything, so I started writing a couple of classes myself.
The interface looks something like this at the moment (altough I'm not finished with it).
PHP Code:
<?php
$mgr = new nc_File_Upload_Manager( $_FILES );
$mgr->setTargetDir( '/path/to/upload/dir' );
// We want to handle all images, and all MS Word documents
$mgr->addAllowedMime( new nc_File_ImageMimes() );
$mgr->addAllowedMime( 'application/msword' );
// We want all images, except bitmaps
$mgr->addUnallowedMime( 'image/bmp' );
// Add an observer that resizes the images
$resize = new Some_Resize_Class();
$resize->setMaxHeight( 100 );
$observer = new nc_File_Upload_ImageResizeObserver( $resize );
// An observer can be attached to a mime like this:
$mgr->addObserver( $observer, 'image/jpeg', 'after' );
// Or:
$mgr->addObserver( $observer, Array( 'image/jpeg', 'image/png' ), 'after' );
// Or, to attach it to all web images and 'image/bmp':
$mgr->addObserver( $observer, new nc_File_WebImageMimes( 'image/bmp' ), 'after' );
// Or even:
$mgr->addObserver( $observer, new nc_File_WebImageMimes( new nc_File_DocumentMimes() ), 'after' );
try
{
$file = $mgr->upload( 'upload_field' );
// Or, if you wish to specify the name yourself
$file = $mgr->upload( 'upload_field', 'myfile.jpg' );
// Or, if you just want to upload all files
$files = $mgr->uploadAll();
// Or, if you just want to upload all files when you have named your inputs
// something like 'upload_field[]'
$files = $mgr->uploadAll( 'upload_field' );
}
catch( nc_File_Upload_Exception $e )
{
echo $e->getMessage();
exit;
}
?>
Some explainations:
Mime checking is done by first checking if the extension fileinfo is availible. If it isn't, a shell command is executed to get the correct mime (only works on *nix), and if the two above fails, it relys on the mime set in $_FILES, which isn't that good.
The observers can be attached to be executed either before the file is moved (when it's in the temp folder) or after it's been moved. An observer just needs to implement the nc_File_Upload_iObserver interface to be attached. An observer can also throw an exception which cancels the upload and removes the file.
The classes like nc_File_WebImageMimes are just wrappers around common mime types, so you don't have to type all of them, but you still have to control to add/remove mime types in these collections.
It's not by far finshed yet, and I'm still working on the unit tests, but if anyone is interested, I'd be happy to share the code.
And also, if anyone has any feedback on the interface, I'd be happy to hear it!
Bookmarks