Flourish PHP Unframework

fFile

class, implements Iterator Countable , v1.0.0b39

Represents a file on the filesystem, also provides static file-related methods

Changes:
1.0.0b39Backwards Compatibility Break - output() now automatically ends any open output buffering and discards the contents 8/24/11
1.0.0b38Added the Countable interface to the class 6/3/11
1.0.0b37Fixed mime type detection of BMP images 3/7/11
1.0.0b36Added the $remove_extension parameter to getName() 1/10/11
1.0.0b35Added calls to clearstatcache() in append() and write() to prevent incorrect data from being returned by getMTime() and getSize() 11/27/10
1.0.0b34Added getExtension() 5/10/10
1.0.0b33Fixed another situation where rename() with the same name would cause the file to be deleted 4/13/10
1.0.0b32Fixed rename() to not fail when the new and old filename are the same 3/16/10
1.0.0b31Added append() 3/15/10
1.0.0b30Changed the way files deleted in a filesystem transaction are handled, including improvements to the exception that is thrown 3/5/10
1.0.0b29Fixed a couple of undefined variable errors in determineMimeTypeByContents() 3/3/10
1.0.0b28Added support for some JPEG files created by Photoshop 12/16/09
1.0.0b27Backwards Compatibility Break - renamed getFilename() to getName(), getFilesize() to getSize(), getDirectory() to getParent(), added move() 12/16/09
1.0.0b26getDirectory(), getFilename() and getPath() now all work even if the file has been deleted 10/22/09
1.0.0b25Fixed __construct() to throw an fValidationException when the file does not exist 8/21/09
1.0.0b24Fixed a bug where deleting a file would prevent any future operations in the same script execution on a file or directory with the same path 8/20/09
1.0.0b23Added the ability to skip checks in __construct() for better performance in conjunction with fFilesystem::createObject() 8/6/09
1.0.0b22Fixed __toString() to never throw an exception 8/6/09
1.0.0b21Fixed a bug in determineMimeType() 7/21/09
1.0.0b20Fixed the exception message thrown by output() when output buffering is turned on 6/26/09
1.0.0b19rename() will now rename the file in its current directory if the new filename has no directory separator 5/4/09
1.0.0b18Changed __sleep() to not reset the iterator since it can cause side-effects 5/4/09
1.0.0b17Added __sleep() and __wakeup() for proper serialization with the filesystem map 5/3/09
1.0.0b16output() now accepts TRUE in the second parameter to use the current filename as the attachment filename 3/23/09
1.0.0b15Added support for mime type detection of MP3s based on the MPEG-2 (as opposed to MPEG-1) standard 3/23/09
1.0.0b14Fixed a bug with detecting the mime type of some MP3s 3/22/09
1.0.0b13Fixed a bug with overwriting files via rename() on Windows 3/11/09
1.0.0b12Backwards compatibility break - Changed the second parameter of output() from $ignore_output_buffer to $filename 3/5/09
1.0.0b11Changed __clone() and duplicate() to copy file permissions to the new file 1/5/09
1.0.0b10Fixed duplicate() so an exception is not thrown when no parameters are passed 1/5/09
1.0.0b9Removed the dependency on fBuffer 1/5/09
1.0.0b8Added the Iterator interface, output() and getMTime() 12/17/08
1.0.0b7Removed some unnecessary error suppresion operators 12/11/08
1.0.0b6Added the __clone() method that duplicates the file on the filesystem when cloned 12/11/08
1.0.0b5Fixed detection of mime type for JPEG files with Exif information 12/4/08
1.0.0b4Changed the constructor to ensure the path is to a file and not directory 11/24/08
1.0.0b3Fixed mime type detection of Microsoft Office files 11/23/08
1.0.0b2Made rename() and write() return the object for method chaining 11/22/08
1.0.0bThe initial implementation 6/14/07

Genealogy

Child Classes

fImage
Represents an image on the filesystem, also provides image manipulation functionality

Variables

->deleted protected

A backtrace from when the file was deleted

Type

array

->file protected

The full path to the file

Type

string

Static Methods

::create() public

Creates a file on the filesystem and returns an object representing it.

This operation will be reverted by a filesystem transaction being rolled back.

Signature

fFile create( string $file_path, string $contents )

Parameters

string $file_path The path to the new file
string $contents The contents to write to the file, must be a non-NULL value to be written

Throws

fValidationException
When no file was specified or the file already exists

Overridden By

fImage::create()
Creates an image on the filesystem and returns an object representing it

::determineMimeType() 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

Determines the file's mime type by either looking at the file contents or matching the extension

Please see the getMimeType() description for details about how the mime type is determined and what mime types are detected.

Signature

string determineMimeType( string $file, string $contents=NULL )

Parameters

string $file The file to check the mime type for - must be a valid filesystem path if no $contents are provided, otherwise just a filename
string $contents The first 4096 bytes of the file content - the $file parameter only need be a filename if this is provided

Returns

The mime type of the file

Methods

->__construct() public

Creates an object to represent a file on the filesystem

If multiple fFile objects are created for a single file, they will reflect changes in each other including rename and delete actions.

Signature

fFile __construct( string $file, boolean $skip_checks=FALSE )

Parameters

string $file The path to the file
boolean $skip_checks If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere

Throws

fValidationException
When no file was specified, the file does not exist or the path specified is not a file

Overridden By

fImage::__construct()
Creates an object to represent an image on the filesystem

->__clone() 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

Duplicates a file in the current directory when the object is cloned

Signature

fFile __clone( )

Returns

The new fFile object

->__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

->__sleep() 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

The iterator information doesn't need to be serialized since a resource can't be

Signature

array __sleep( )

Returns

The instance variables to serialize

->__toString() public

Returns the filename of the file

Signature

string __toString( )

Returns

The filename

->__wakeup() 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

Re-inserts the file back into the filesystem map when unserialized

Signature

void __wakeup( )

->append() public

Appends the provided data to the file

If a filesystem transaction is in progress and is rolled back, this data will be removed.

Signature

fFile append( mixed $data )

Parameters

mixed $data The data to append to the file

Returns

The file object, to allow for method chaining

Overridden By

fImage::append()
Prevents a programmer from trying to append an image

->count() public implements Countable

Returns the number of lines in the file

Signature

integer count( )

Returns

The number of lines in the file

->current() internal public implements Iterator

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

Returns the current line of the file (required by iterator interface)

Signature

array current( )

Returns

The current row

Throws

fNoRemainingException
When there are no remaining lines in the file

->delete() public

Deletes the current file

This operation will NOT be performed until the filesystem transaction has been committed, if a transaction is in progress. Any non-Flourish code (PHP or system) will still see this file as existing until that point.

Signature

void delete( )

->duplicate() public

Creates a new file object with a copy of this file

If no directory is specified, the file is created with a new name in the current directory. If a new directory is specified, you must also indicate if you wish to overwrite an existing file with the same name in the new directory or create a unique name.

This operation will be reverted by a filesystem transaction being rolled back.

Signature

fFile duplicate( string|fDirectory $new_directory=NULL, boolean $overwrite=NULL )

Parameters

string|fDirectory $new_directory The directory to duplicate the file into if different than the current directory
boolean $overwrite If a new directory is specified, this indicates if a file with the same name should be overwritten.

Returns

The new fFile object

->getExtension() public

Gets the file extension

Signature

string getExtension( )

Returns

The extension of the file

->getMimeType() public

Gets the file's mime type

This method will attempt to look at the file contents and the file extension to determine the mime type. If the file contains binary information, the contents will be used for mime type verification, however if the contents appear to be plain text, the file extension will be used.

The following mime types are supported. All other binary file types will be returned as application/octet-stream and all other text files will be returned as text/plain.

Archive:

  • application/x-bzip2 BZip2 file
  • application/x-compress Compress (*nix) file
  • application/x-gzip GZip file
  • application/x-rar-compressed Rar file
  • application/x-stuffit StuffIt file
  • application/x-tar Tar file
  • application/zip Zip file

Audio:

  • audio/x-flac FLAC audio
  • audio/mpeg MP3 audio
  • audio/mp4 MP4 (AAC) audio
  • audio/vorbis Ogg Vorbis audio
  • audio/x-wav WAV audio
  • audio/x-ms-wma Windows media audio

Document:

  • application/vnd.ms-excel Excel (2000, 2003 and 2007) file
  • application/pdf PDF file
  • application/vnd.ms-powerpoint Powerpoint (2000, 2003, 2007) file
  • text/rtf RTF file
  • application/msword Word (2000, 2003 and 2007) file

Image:

  • image/x-ms-bmp BMP file
  • application/postscript EPS file
  • image/gif GIF file
  • application/vnd.microsoft.icon ICO file
  • image/jpeg JPEG file
  • image/png PNG file
  • image/tiff TIFF file
  • image/svg+xml SVG file

Text:

  • text/css CSS file
  • text/csv CSV file
  • text/html (X)HTML file
  • text/calendar iCalendar file
  • application/javascript Javascript file
  • application/x-perl Perl file
  • application/x-httpd-php PHP file
  • application/x-python Python file
  • application/rss+xml RSS feed
  • application/x-ruby Ruby file
  • text/tab-separated-values TAB file
  • text/x-vcard VCard file
  • application/xhtml+xml XHTML (Real) file
  • application/xml XML file

Video/Animation:

  • video/x-msvideo AVI video
  • application/x-shockwave-flash Flash movie
  • video/x-flv Flash video
  • video/x-ms-asf Microsoft ASF video
  • video/mp4 MP4 video
  • video/ogg OGM and Ogg Theora video
  • video/quicktime Quicktime video
  • video/x-ms-wmv Windows media video

Signature

string getMimeType( )

Returns

The mime type of the file

->getMTime() public

Returns the last modification time of the file

Signature

fTimestamp getMTime( )

Returns

The timestamp of when the file was last modified

->getName() public

Gets the filename (i.e. does not include the directory)

Signature

string getName( boolean $remove_extension=FALSE )

Parameters

boolean $remove_extension If the extension should be removed from the filename

Returns

The filename of the file

->getParent() public

Gets the directory the file is located in

Signature

fDirectory getParent( )

Returns

The directory containing the file

->getPath() public

Gets the file's current path (directory and filename)

If the web path is requested, uses translations set with fFilesystem::addWebPathTranslation()

Signature

string getPath( boolean $translate_to_web_path=FALSE )

Parameters

boolean $translate_to_web_path If the path should be the web path

Returns

The path (directory and filename) for the file

->getSize() public

Gets the size of the file

The return value may be incorrect for files over 2GB on 32-bit OSes.

Signature

integer|string getSize( boolean $format=FALSE, integer $decimal_places=1 )

Parameters

boolean $format If the filesize should be formatted for human readability
integer $decimal_places The number of decimal places to format to (if enabled)

Returns

If formatted a string with filesize in b/kb/mb/gb/tb, otherwise an integer

->isWritable() public

Check to see if the current file is writable

Signature

boolean isWritable( )

Returns

If the file is writable

->key() internal public implements Iterator

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

Returns the current one-based line number (required by iterator interface)

Signature

integer key( )

Returns

The current line number

Throws

fNoRemainingException
When there are no remaining lines in the file

->move() public

Moves the current file to a different directory

Please note that rename() will rename a file in its directory or rename it into a different directory.

If the current file's filename already exists in the new directory and the overwrite flag is set to false, the filename will be changed to a unique name.

This operation will be reverted if a filesystem transaction is in progress and is later rolled back.

Signature

fFile move( fDirectory|string $new_directory, boolean $overwrite )

Parameters

fDirectory|string $new_directory The directory to move this file into
boolean $overwrite If the current filename already exists in the new directory, TRUE will cause the file to be overwritten, FALSE will cause the new filename to change

Returns

The file object, to allow for method chaining

Throws

fValidationException
When the directory passed is not a directory or is not readable

->next() internal public implements Iterator

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

Advances to the next line in the file (required by iterator interface)

Signature

void next( )

Throws

fNoRemainingException
When there are no remaining lines in the file

->output() public

Prints the contents of the file

This method is primarily intended for when PHP is used to control access to files.

Be sure to close the session, if open, to prevent performance issues. Any open output buffers are automatically closed and discarded.

Signature

fFile output( boolean $headers, mixed $filename=NULL )

Parameters

boolean $headers If HTTP headers for the file should be included
mixed $filename Present the file as an attachment instead of just outputting type headers - if a string is passed, that will be used for the filename, if TRUE is passed, the current filename will be used

Returns

The file object, to allow for method chaining

->read() public

Reads the data from the file

Reads all file data into memory, use with caution on large files!

This operation will read the data that has been written during the current transaction if one is in progress.

Signature

string read( )

Returns

The contents of the file

->rename() public

Renames the current file

If the filename already exists and the overwrite flag is set to false, a new filename will be created.

This operation will be reverted if a filesystem transaction is in progress and is later rolled back.

Signature

fFile rename( string $new_filename, boolean $overwrite )

Parameters

string $new_filename The new full path to the file or a new filename in the current directory
boolean $overwrite If the new filename already exists, TRUE will cause the file to be overwritten, FALSE will cause the new filename to change

Returns

The file object, to allow for method chaining

->rewind() internal public implements Iterator

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

Rewinds the file handle (required by iterator interface)

Signature

void rewind( )

->tossIfDeleted() protected

Throws an fProgrammerException if the file has been deleted

Signature

void tossIfDeleted( )

->valid() internal public implements Iterator

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

Returns if the file has any lines left (required by iterator interface)

Signature

boolean valid( )

Returns

If the iterator is still valid

->write() public

Writes the provided data to the file

Requires all previous data to be stored in memory if inside a transaction, use with caution on large files!

If a filesystem transaction is in progress and is rolled back, the previous data will be restored.

Signature

fFile write( mixed $data )

Parameters

mixed $data The data to write to the file

Returns

The file object, to allow for method chaining