Object wrapper for PHPs file handling functions. Has implementation for file_get/file_put contents, fopen/fread/write/fclose and other properties. Class will also set file properties (owner, group permissions etc) on file load.
$oFile = new fileObject('/path/to/file.extn');
$oFile->get();
Contains a parseDir() method to recursively fetch files from a folder. Found files are returned as file objects.
$files = fileObject::parseDir('/home/user/public_html');
foreach ( $files as $oFile ) {
echo $oFile->getFilename()."\n";
}
public __construct([$inFilename = false])
Returns a new file object, if $filename is specifed a check is made to see if the file exists, if so the file properties are loaded
public static getInstance($inFilename)
Returns an instance of fileObject for the supplied $inFilename
public static cleanFileName($inFilename)
Removes all illegal characters from $inFilename leaving only a-zA-Z0-9 and _-. as legal characters
public static parseDir($inDir, [$inRecurse = true])
Parses $inDir for files and returns an array of fileObjects, if $inRecurse is true (Default) $inDir is recursed
public static countFilesInDir($inDir, [$inType = 'file'])
Returns the file or directory count of $inDir
$inType is either "file" or "dir" to fetch only files or directory counts. This method does not include the dot folders (. & ..) from Unix filesystems.
public setFileProperties()
Sets the internal object properties for the file
public getOriginalFilename()
Returns the original filename
public getPath()
Returns the file path
public getFilename()
Returns cleaned filename
public getExtension()
Returns the last part of the filename that should be the extension
public getFilesize()
Returns filesize in bytes
public lastModified()
Returns last modified timestamp
public getOwner()
Returns current owner
public getGroup()
Returns current group
public getPermissions()
Returns permissions for file
public getData()
Returns file data from the object
public isReadable()
Returns if the file is readable
public isWritable()
Returns if the file is writable
public isExecutable()
Returns if the file is executable
public isDir()
Returns if the file is a directory
public exists()
Returns if the file exists
public open([$inMode = 'r'])
Opens a file as a resource for reading / writing / streaming; $mode is any valid value for fopen() mode
public read([$inBytes = 1024])
Reads data from an open file resource in $bytes chunks
public readStream([$inBytes = -1])
Reads an open file resource as a stream via stream_get_contents() in $bytes chunks where -1 is everything
public eof()
Returns if end of file has been reached
public write([$inData = false], [$inBytes = 1024])
Write $data, or already loaded data to an open file resource at $bytes chunks
public close()
Close open file resource
public get()
Wrapper for file_get_contents(), fetches the whole loaded file, stores the data internally and returns it
public put([$inData = false], [$inFlags = false])
Wrapper for file_put_contents(), writes $data or existing data to file and returns bytes written or false $flags takes any valid flag for www.php.net/file_put_contents e.g. LOCK_EX
public delete()
Delete loaded file from the filesystem if it exists
public setGroup([$inGroup = false])
Set group for file
public setOwner([$inOwner = false])
Set owner of file
public setPermissions([$inPermissions = false])
Set permissions to assign to file on writing, or if file exists, assign permissions
public setFilename($inFilename)
Set filename for new file
public setData($inData)
Set file data to write
public reset()
Resets object properties
Posted by: Scorpio Documentor (Writer), in File on 19 Nov 2009 @ 20:30
Tags: file, fileobject,
This
work is licenced under a
Creative Commons Licence.