Simplified wrapper for file uploading.

package dstruct_filetools
author David Lidstone

 Methods

Class constructor.

__construct() 

Mime type of the file.

contentType(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Count uploaded files.

count() : integer

Files with 0 size are NOT counted.

Returns

integer

Does a file by the key exist.

exists(string $key) : boolean

The key is the internal 'name' of the file - usually specified by the name attribute of your form's file element

Parameters

$key

string

{@link setDefault()}

Returns

boolean

Extension part of the uploaded file's name.

extension(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Full name of the uploaded file.

fullName(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Returns the key of the default file.

getDefault() : string

The default file will be the first file uploaded or the file manually set to default. The key is used by other methods of this class.

see \setDefault()

Returns

string

Error string if generated by the upload process.

getErrorMessage(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Error number if generated by the upload process.

getErrorNumber(string $key) : integer

Parameters

$key

string

{@link setDefault()}

Returns

integer

Get whether errors will be shown if no file is uploaded.

getFileRequired() : boolean

Get the name of the saved file.

getNewName() : string

Will be empty if the file has not been saved yet.

see \setNewName()

Returns

string

Set the character used when file already exists.

getOverwriteChar() : string
see \setOverwriteChar()

Returns

string

Set the overwrite mode for existing files.

getOverwriteMode() : integer
see \setOverwriteMode()

Returns

integer

Get the path of the saved file.

getSavePath() : string

Will be empty if the file has not been saved yet.

see \setSavePath()

Returns

string

Get the name of the file in its temporary folder.

getTempFile(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Fetch and errors found during the upload process.

getUploadErrors() : array
see \setUploadErrors()

Returns

array

Check the file type is allowable.

isAllowableType(string $key) : boolean

Checks both extension and mime against any provided arrays: setAllowableTypes

Parameters

$key

string

{@link setDefault()}

Returns

boolean

Check for errors in the upload process.

isError(string $key) : boolean

Return depends on whether an uploaded file is required. See setFileRequired().

Parameters

$key

string

{@link setDefault()}

Returns

boolean

Get the name part of the uploaded file (no extension).

nameOnly(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

Saves the file with the previously specified parameters.

save(string $key) : boolean
todo Should throw a descriptive error if can not save

Parameters

$key

string

{@link setDefault()}

Returns

booleanTrue on success

Limit allowable file types.

setAllowableTypes(array $mimes, array $extns) 

Types must be in lower-case for the comparision to work

Parameters

$mimes

array

Allowable Mime-Types

$extns

array

Allowable file-extensions

Set a default uploaded file to be used.

setDefault(string $key) : boolean

For example, if your form field is 'userfile', you can set this to 'userfile' and not need to keep supplying a key.

Parameters

$key

string

Returns

booleanFalse if no file by that name exists

Does the script expect errors if no file is uploaded?

setFileRequired(boolean $bln) 
see \FileUploader::isErrors()

Parameters

$bln

boolean

Set the new name of the file manually.

setNewName(string $name) 

The file name is usually preserved if a file by that name is not already present (@see setOverwriteMode()}. This allows setting of the name manually before saving. Use the full name, excluding the extension.

Parameters

$name

string

Sets a character to use when in Overwrite mode.

setOverwriteChar(string $char) 

If setOverwriteMode() is set to OVERWRITE_APPEND, files are not overwritten but have this overwrite character and a number appened to the file name. Default is _ (underscore).
Be wary of restrictions due to operating systems.

Parameters

$char

string

Overwrite mode for saving files.

setOverwriteMode(integer $mode) 

Overwrite can be one of OVERWRITE_NO, OVERWRITE_YES or OVERWRITE_APPEND. Default is OVERWRITE_NO. If NO, any existing file will not be saved over, and save() will return false if a file already exists. If YES, any existing file will be overwritten without notice. If APPEND and a file exists, the overwrite character (see setOverwriteChar()) and a number is appended to the file. The number will be the next free integer, starting at 1. To find the final name the file is given, use getNewName().

Parameters

$mode

integer

OVERWRITE_ Constant as above

Set the path to save files.

setSavePath(string $path) : boolean

Must be from absolute route e,g, /var/www.... or d:\domains... WARNING: Do not let users enter the save path. Untested, but if this is allowable...
"../../../place_my_evil.exe"
then could be very dangerous! Even if tested and it doesn't work, it could be using Absolute Path mode!
Also, you may need to call clearstatcache()

todo Test for relative path breakouts
todo Add parameter to call clearstatcache()?

Parameters

$path

string

Returns

booleanSuccess or failure when setting path (i.e. path exists)

Customise error messages.

setUploadErrors(array $errors) 

Replaces $uploaderrors array. Must have same format

todo Remove if we can build in PHP's errors instead of copying here

Parameters

$errors

array

Size of the file in ?bytes?

size(string $key) : integer
todo check return is in bytes

Parameters

$key

string

{@link setDefault()}

Returns

integer

Returns default, first or specified key.

validKey(string $key) : string

Parameters

$key

string

{@link setDefault()}

Returns

string

 Properties

 

$defaultkey : integer
 

$extensions : array
 

$mimetypes : array
 

$newname : string
 

$overwritechar : String
 

$overwritemode : integer
 

$requirefile : \unknown
 

$savepath : string

This should be defined from the root of the filesystem.

 

$totalfiles : integer
 

$uploaderrors : array
todo Can't we use the built in constants???

 Constants

 

Append a number to the file name if there is an existing file.

OVERWRITE_APPEND : integer
 

Overwrite existing files.

OVERWRITE_NO : integer
see \FileUploader::setOverwriteMode()
todo sort methods alphabetically
 

Don't overwrite existing files.

OVERWRITE_YES : integer