Class: File
- Package: saf.File
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 2.2, 2003-09-29, $Id: File.php,v 1.3 2007/10/06 00:06:30 lux Exp $
- Access: public
File is a simple class to keep basic file information gathering structured
and readable. Stores everything as a property of the class instead of as methods, so
that it integrates easily with the Template class.
New in 1.2:
- Added $filemtime, $gid, $uid, $perms, $filesize, $formatted_perms, $group, and $owner
properties, and a formatPerms() method.
New in 1.4:
- Added setStatus(), getStatus(), and clearStatus() methods.
New in 1.6:
- Added a $content property and a contents() method.
New in 1.8:
- Happy St. Valentine's day!
- Had a fling with a Windows bug today. :)
New in 2.0:
- Removed getStatus(), setStatus(), and clearStatus(). The status functionality
is now part of saf.App.Versioning.Rev.
New in 2.2:
- Added overwrite() and append() methods, and functions file_overwrite() and
file_append() as aliases to them.
Usage Example
<?php
$file = new File ('/absolute/path/to/file.txt', '/web/path');
// check if the file is a directory
if ($file->is_dir) {
// file is a directory
}
// output the file's name, size and date it was last modified
echo $file->name;
echo $file->size;
echo $file->last_modified;
?>
Return to Top
Properties
$handle = ""
Contains the handle of the currently open directory.
$path = ""
The web path to the file, beginning from the document root.
$_ls = array ()
Contains the list of files retrived from the previous read_all()
method call.
$description = "Unknown Document Type"
The description of the file.
$icon = "pix/icons/unknown.gif"
An icon to help describe the file visually.
$type = "ascii"
Type can be either 'ascii', 'binary', or 'folder'. Sitellite
treats files differently depending on their type.
$name = ""
The file's name.
$extension = ""
The file's extension (ie. the extension of 'file.gif' is 'gif').
$absolute = ""
The absolute path to the file, including its name.
$filesize
The actual size of this file in bytes.
$size = ""
The size of the file in a formatted string.
$filemtime
The actual value returned by the filemtime() call on this file.
$last_modified = ""
The date and time the file was last modified, in a formatted string.
$gid
The group id of the file.
$uid
The user id of the file.
$perms
The octal permissions of the file.
$formatted_perms
The permissions of the file formatted by the formatPerms()
method.
$group = ''
The group name of the file. Not available if PHP is running
in safe_mode.
$owner = ''
The owner name of the file. Not available if PHP is running
in safe_mode.
$is_writeable = false
Determines whether or not this file is writeable by the
current script.
$is_dir = false
Determines whether or not this file is a directory.
$content = false
Stores the content of the file after a call to contents().
Return to Top
Methods
File ($abs_file_path = '', $www_file_path = '')
Constructor method. $abs_file_path is the absolute directory path
to the file, including its name. $www_file_path is the path from the web
document root to same, minus the file name.
format_filesize ($size = 0)
- Access: public
- Return: string
Formats the size of the file into a string appropriate for human consumption.
get_mime ($db = "")
Gets the file's mime data from the database. Sets the values for
the following properties: description, icon and type.
$db is an optional Database object. If not supplied, File will look
for a Database object called $db in the global namespace.
contents ($file = '')
- Access: public
- Return: string
Returns the contents of the file as a string. If the $file is
not specified, it uses the $absolute property of the current File
object. Also stores it in the $content property so you don't have
to read it from the file system twice.
contains ($string, $regex = 0, $file = '', $count = false, $filter = false)
- Access: public
- Return: boolean
Opens a file and returns whether or not it contains a specified
string or pattern. Can be called using the File::contains syntax, as
well as through an instantiated object.
formatPerms ($mode)
- Access: public
- Return: string
Reads an octal mode value and returns a string of the
format -rwxrwxrwx, similar to what is returned by an "ls -al"
command on a Unix command prompt. This method was borrowed
from the user-contributed notes at http://www.php.net/fileperms
overwrite ($data, $file = '')
- Access: public
- Return: boolean
Overwrite the specified file (or the file contained in the $absolute
property) with the specified $data.
append ($data, $file = '')
- Access: public
- Return: boolean
Append to the specified file (or the file contained in the $absolute
property) with the specified $data.
rand ($path, $ext = false)
- Access: public
- Return: string
Select a random file from the specified directory. The $ext is
an optional extension, or array of extensions, to limit the
selection to. Also automatically eliminates dot-files and folders.
determine ($list, $path = false)
- Access: public
- Return: string
Selects the first file that exists from the specified list.
This can be used to cascade possible choices for displaying
images, including templates, or other context-sensitive
data.
Return to Top