This package provides functionality to manipulate files on the server. This includes type and size validation, uploading, image resizing, MP3 - ID3 tag parsing, etc..
The need for this package really came up around the third time I had to accept file uploads from a site… first, there is a lot of steps you have to take that the file is actually there and valid (depending on varying criteria), and then I could never remember the proper syntax for moving files. This doesn’t even include validating target destinations and creating directories.. Pretty annoying!
So as you can see, plenty of opportunity for encapsulation. Enter the FileSystem Package…
The FileSystem Package consists of the following classes:
- File
- FileUpload
- ImageUpload
- ImageResize
- MultiImageResize
- MP3Upload
Download FileSystem Package | Browse Package Documentation
Here are a few examples of how I use these classes:
require_once “com/filesystem/FileUpload.class.php”;
if( $submitted )
{
$File = new FileUpload(”fieldname”);
$File -> setFileTypes( array(”pdf”, “doc” ) );
$File -> setMaxSize( 300000 );
if( $File -> exists() && !$File -> isValid() )
{
$errormsg = $File -> getErrormsg();
}
else
{
$File -> upload(”upload_dir”, “new_name” );
}
As you can see, this makes for a flexible syntax that handles validation and directory checking behind the scenes. Ideally you want to wrap this code into a try block, since directory violations throw Exceptions of the type FileException.
Note: after the file has been uploaded, $File -> getPath() returns the new path, so you can continue working with it.