Class FileUpload

Description

The FileUpload class can be used to easily manage file uploads with the PHP engine. The FileUpload class can be used individually or can be used together with the FileUploadController class for more functionality.

Located in /fileupload.class.php (line 14)


	
			
Variable Summary
array $aErrors
array $aUploaded
boolean $bMultiple
integer $iMaxSize
string|integer $mStartNumber
object $oFileObject
string $sDirectory
null|string|array $sNewName
Method Summary
void FileUpload (object &$oFileObject)
void addError (string $sFileName, integer $iFileSize, integer $iErrorCode)
void addFileType (mixed $mFileType, [mixed $mMimeTypes = null])
void addUploaded (string $sFileName, integer $iFileSize, [string $sNewName = ''], [string $sMessage = ''])
void generateDirectoryStructure (string $sDirPath, [mixed $sChmod = '0777'])
mixed getErrors ()
string getExtension (string $sFileName)
integer getHighestFileNumber (string $sDirPath)
array getUploaded ()
boolean isEmptyDir (string $sDirPath)
boolean isFileTypeAllowed (string $sFileName, string $sMimeType)
void move (string $sDirPath)
string prepareFileName (string $sFileName)
array readDirectory (string $sDirPath, [boolean $bLoseExtensions = true], [array $aSkipList = array('.', '..')])
void rollback ()
void setIgnoreEmptyUploads ([boolean $bIgnore = true])
void setLowerCaseExtension ([boolean $bSet = true])
void setMaxSize (integer $iMaxSize)
void setNewName (string|array $sNewName)
void setStartNumber ([mixed $mNumber = 'auto'])
Variables
array $aAllowedFileTypes = array() (line 19)
  • var: All the filetypes (extension, mime-types) are stored here
array $aErrors = array() (line 29)
  • var: Stores the errors (if any) which are encountered
array $aUploaded = array() (line 24)
  • var: All the info about the uploaded files is stored in this array
boolean $bIgnoreEmptyUploads = false (line 64)
  • var: If set to true, empty uploads will not be classified as an error
boolean $bLowerCaseExtension = false (line 59)
  • var: If set to true all extension will be casted to lowercase
boolean $bMultiple = false (line 54)
  • var: Is set to true when an array of files is being uploaded
integer $iMaxSize = 0 (line 44)
  • var: Sets the maximum allowed filesize of each file in bytes
string|integer $mStartNumber = 1 (line 49)
  • var: Sets the number where the automatic numbering system must start counting
object $oFileObject (line 69)
  • var: The oFileObject stores the reference to the file object provided by the contructor
string $sDirectory (line 39)
  • var: Stores the path to the directory were all files should be stored
null|string|array $sNewName = null (line 34)
  • var: Holds the new name for an individual file or an array of names for multiple upload
Methods
Constructor FileUpload (line 81)

The contructor loads the $_FILES array as an reference into a local variable inside the class.

void FileUpload (object &$oFileObject)
  • object $oFileObject: A reference to the $_FILES array
addError (line 124)

Adds a new error to the array

void addError (string $sFileName, integer $iFileSize, integer $iErrorCode)
  • string $sFileName: The name of the file
  • integer $iFileSize: The size of the file in bytes
  • integer $iErrorCode: The errorcode
addFileType (line 101)

Valid filetypes can be added to the object by using this function.

The function's first argument can accept a string together with an array of mimetypes as the second argument. Else one can input an array with extension => array(mimetypes) as the first argument.

void addFileType (mixed $mFileType, [mixed $mMimeTypes = null])
  • mixed $mFileType: The extension of the file or the complete array
  • mixed $mMimeTypes: The array of mimetypes if the first argument is a string
addUploaded (line 163)

Adds a new uploaded file to the array

void addUploaded (string $sFileName, integer $iFileSize, [string $sNewName = ''], [string $sMessage = ''])
  • string $sFileName: The name of the file
  • integer $iFileSize: The size of the file in bytes
  • string $sNewName: The new name the file has been given
  • string $sMessage: An additional message
generateDirectoryStructure (line 344)

Generates a directory structure on the server according to the full path given in the parameter. existing directories aren't touched.

New directories will be given default chmod.

void generateDirectoryStructure (string $sDirPath, [mixed $sChmod = '0777'])
  • string $sDirPath: The full path
getErrors (line 287)

This function returns an array with errors if they occured, if no errors were encountered this function will return false. Remember that rollback entries will only be entered after the move() has been initiated.

mixed getErrors ()
getExtension (line 312)

Extracts the extension of the filename provided

string getExtension (string $sFileName)
  • string $sFileName: The full name of the file
getHighestFileNumber (line 582)

Returns the highest filenumber in the given directory. If the directory is empty, it will return the value 0.

integer getHighestFileNumber (string $sDirPath)
  • string $sDirPath
getUploaded (line 299)

Returns the array with succesfull uploads

array getUploaded ()
isEmptyDir (line 554)

Checks if a given directory is empty (has no files/dirs in it)

  • return: Returns true if directory is empty, else false
  • since: 2.0
  • version: 1.0
  • author: Gerard Klomp <gerard@theprodukt.com>
boolean isEmptyDir (string $sDirPath)
  • string $sDirPath
isFileTypeAllowed (line 326)

Checks if the file extension and mimetype are allowed

  • return: Returns true if extension and mimetype are allowed, else false
  • since: 2.0
  • version: 1.0
  • author: Gerard Klomp
boolean isFileTypeAllowed (string $sFileName, string $sMimeType)
  • string $sFileName: The full name of the file
  • string $sMimeType: The MIME-Type of the file
move (line 387)

This is the main function which checks all the factors of the file(s) and moves them.

void move (string $sDirPath)
  • string $sDirPath: The full path where the files should be stored
prepareFileName (line 369)

This function prepares the filename. It checks if the extension should be casted to lowercase and returns the correct filename.

string prepareFileName (string $sFileName)
  • string $sFileName
readDirectory (line 605)

Lists the contents of a given directory.

array readDirectory (string $sDirPath, [boolean $bLoseExtensions = true], [array $aSkipList = array('.', '..')])
  • string $sDirPath: The path to the directory to be listed
  • boolean $bLoseExtensions: Should extensions be stripped from the filenames
  • array $aSkipList: An array of items which should not be listed
rollback (line 520)

Rollback deletes any uploaded pictures and destroys the empty directories which are left. It does not touch any other directories which are not empty and it doesn't delete any directories other than the ones provided when issuing the move() command.

void rollback ()
setIgnoreEmptyUploads (line 254)

If the bIgnoreEmptyUploads is set to true, errors which are caused

with the errorcode 4 (no file uploaded) will not be added to the error array. This can be usefull if you use a static ammount of fields for multiple file uploads and you're using the rollback() function if any errors occured. Because a not-uploaded error doesn't always have to be a real error.

void setIgnoreEmptyUploads ([boolean $bIgnore = true])
  • boolean $bIgnore
setLowerCaseExtension (line 237)

If the bLowerCaseExtension is set to true, extensions of file uploads will be casted to lowercase. This can be usefull if you do not wish to check if the file has the extension .JPG or .jpg for example.

void setLowerCaseExtension ([boolean $bSet = true])
  • boolean $bSet: Set to true when extensions should be casted
setMaxSize (line 181)

Sets the maximum size in bytes the uploaded file(s) must comply to

void setMaxSize (integer $iMaxSize)
  • integer $iMaxSize: The maximum size in bytes
setNewName (line 208)

Sets the new name(s) or naming method for the files being uploaded.

If only one file is being uploaded and the first argument is a string that name will be used for the uploaded file.

If multiple files are being uploaded you can feed an array of names to this function which will be used for the uploaded files. If your array with names is shorter than the number of files being uploaded the class will use the original name for the files remaining.

You also have the option to enter 'alpha' or 'num' as a string when uploading an array of files. The new names of the files will be according to the type selected (alpha starts with 'a' and num starts with 1). This function can be usefull if you are using the class for an image gallery.

void setNewName (string|array $sNewName)
  • string|array $sNewName: New name for an individual file/names for multiple files or the naming method
setStartNumber (line 272)

This function sets the startnumber from where the automatic numbering system should start counting.

If you have enabled the automatic numbering and you set the startnumber to 8 (example) the first uploaded file will get the number 8. If you set the startnumber to 'auto' it will autodetect the highest number a file has in the directory and will take that number +1 as a startnumber.

void setStartNumber ([mixed $mNumber = 'auto'])
  • mixed $mNumber

Documentation generated on Fri, 04 Aug 2006 17:49:23 +0200 by phpDocumentor 1.3.0RC3