Flourish PHP Unframework

fUpload

class, v1.0.0b15

Provides validation and movement of uploaded files

Changes:
1.0.0b15Fixed an undefined variable error in setMaxSize() 9/16/12
1.0.0b14Fixed some method signatures 8/24/11
1.0.0b13Changed the class to throw fValidationException objects instead of fProgrammerException objects when the form is improperly configured - this is to prevent error logs when bad requests are sent by scanners/hackers 8/24/11
1.0.0b12Fixed the filter() callback constant 11/24/10
1.0.0b11Added setImageDimensions() and setImageRatio() 11/11/10
1.0.0b10BackwardsCompatibilityBreak - renamed setMaxFilesize() to setMaxSize() to be consistent with fFile::getSize() 5/30/10
1.0.0b9BackwardsCompatibilityBreak - the class no longer accepts uploaded files that start with a . unless allowDotFiles() is called - added setOptional() 5/30/10
1.0.0b8BackwardsCompatibilityBreak - validate() no longer returns the $_FILES array for the file being validated - added $return_message parameter to validate(), fixed a bug with detection of mime type for text files 5/26/10
1.0.0b7Added filter() to allow for ignoring array file upload field entries that did not have a file uploaded 10/6/09
1.0.0b6Updated move() to use the new fFilesystem::createObject() method 1/21/09
1.0.0b5Removed some unnecessary error suppression operators from move() 1/5/09
1.0.0b4Updated validate() so it properly handles upload max filesize specified in human-readable notation 1/5/09
1.0.0b3Removed the dependency on fRequest 1/5/09
1.0.0b2Fixed a bug with validating filesizes 11/25/08
1.0.0bThe initial implementation 6/14/07

Static Methods

::check() public

Checks to see if the field specified is a valid file upload field

Signature

boolean check( string $field, boolean $throw_exception=TRUE )

Parameters

string $field The field to check
boolean $throw_exception If an exception should be thrown when there are issues with the form

Returns

If the field is a valid file upload field

Throws

fValidationException
If $throw_exception is TRUE and the request was not a POST or the content type is not multipart/form-data

::compose() protected

Composes text using fText if loaded

Signature

string compose( string $message, mixed $component [, ... ] )

Parameters

string $message The message to compose
mixed $component [, ... ] A string or number to insert into the message

Returns

The composed and possible translated message

::count() public

Returns the number of files uploaded to a file upload array field

Signature

integer count( string $field )

Parameters

string $field The field to get the number of files for

Returns

The number of uploaded files

Throws

fValidationException
If the form is not properly configured for file uploads

::filter() public

Removes individual file upload entries from an array of file inputs in $_FILES when no file was uploaded

Signature

array filter( string $field )

Parameters

string $field The field to filter

Returns

The indexes of the files that were uploaded

Throws

fValidationException
If the form is not properly configured for file uploads

Methods

->__get() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

All requests that hit this method should be requests for callbacks

Signature

callback __get( string $method )

Parameters

string $method The method to create a callback for

Returns

The callback for the method requested

->allowDotFiles() public

Sets the upload class to allow files starting with a .

Files starting with . may change the behaviour of web servers, for instance .htaccess files for Apache.

Signature

void allowDotFiles( )

->allowPHP() public

Sets the upload class to allow PHP files

Signature

void allowPHP( )

->enableOverwrite() public

Set the class to overwrite existing files in the destination directory instead of renaming the file

Signature

void enableOverwrite( )

->move() public

Moves an uploaded file from the temp directory to a permanent location

Signature

fFile|NULL move( string|fDirectory $directory, string $field, mixed $index=NULL )

Parameters

string|fDirectory $directory The directory to upload the file to
string $field The file upload field to get the file from
mixed $index If the field was an array file upload field, upload the file corresponding to this index

Returns

An fFile (or fImage) object, or NULL if no file was uploaded

Throws

fValidationException
When the form is not setup for file uploads, the $directory is somehow invalid or validate() thows an exception

->setImageDimensions() public

Sets the allowable dimensions for an uploaded image

Signature

void setImageDimensions( integer $min_width, integer $min_height, integer $max_width=0, integer $max_height=0 )

Parameters

integer $min_width The minimum width - 0 for no minimum
integer $min_height The minimum height - 0 for no minimum
integer $max_width The maximum width - 0 for no maximum
integer $max_height The maximum height - 0 for no maximum

->setImageRatio() public

Sets the allowable dimensions for an uploaded image

Signature

void setImageRatio( numeric $width, numeric $height, string $allow_excess_dimension )

Parameters

numeric $width The minimum ratio width
numeric $height The minimum ratio height
string $allow_excess_dimension The dimension that should allow for excess pixels

->setMaxSize() public

Sets the maximum size the uploaded file may be

This method should be used with the MAX_FILE_SIZE hidden form input since the hidden form input will reject a file that is too large before the file completely uploads, while this method will wait until the whole file has been uploaded. This method should always be used since it is very easy for the MAX_FILE_SIZE post field to be manipulated on the client side.

This method can only further restrict the upload_max_filesize ini setting, it can not increase that setting. upload_max_filesize must be set in the php.ini (or an Apache configuration) since file uploads are handled before the request is handed off to PHP.

Signature

void setMaxSize( string $size )

Parameters

string $size The maximum file size (e.g. 1MB, 200K, 10.5M) - 0 for no limit

->setMIMETypes() public

Sets the file mime types accepted

Signature

void setMIMETypes( array $mime_types, string $message )

Parameters

array $mime_types The mime types to accept
string $message The message to display if the uploaded file is not one of the mime type specified

->setOptional() public

Sets the file upload to be optional instead of required

Signature

void setOptional( )

->validate() public

Validates the uploaded file, ensuring a file was actually uploaded and that is matched the restrictions put in place

Signatures

NULL|string validate( string $field, mixed $index=NULL, boolean $return_message=NULL )

NULL|string validate( string $field, boolean $return_message=NULL )

Parameters

string $field The field the file was uploaded through
mixed $index If the field was an array of file uploads, this specifies which one to validate
boolean $return_message If any validation error should be returned as a string instead of being thrown as an fValidationException

Returns

If $return_message is not TRUE or if no error occurs, NULL, otherwise a string error message

Throws

fValidationException
When the form is not configured for file uploads, no file is uploaded or the uploaded file violates the options set for this object