Handling files with PHP4 - Part 2 - Page 4
August 19, 2002
|
Last months installment covered using PHP for opening and closing
files, displaying, reading and writing to files. This month we'll
look at moving the current position in the file, copying and
deleting files, getting more information about files, and
uploading files.
|
Download the scripts used in this tutorial
here
Returning information about files
There are a number of PHP functions that return useful information
about files. Most of these functions do not work on remote files -
only files present on the local machine. The most commonly used
of these is the file_exists() function, which determines
whether the file actually exists, Solid code should never assume
a file is there, and possibly throw an error if it isn't.
bool file_exists(string filename).
Returns TRUE if the file exists, and FALSE if not.
<?
$filename = "maybe_maybenot.txt";
if (!(file_exists($filename))) {
print "The file $filename does not exist";
}
else {
// continue processing file
}
?>
Similarly, the filemtime() function returns the time the
content of the file was last modified, useful for automating a
"last updated on" feature.
int filemtime(string filename)
For example:
<?
$filename = "index.php";
if (file_exists($filename)) {
$update_timestamp = filemtime($filename);
// modify the timestamp to date format
$update_time = date("d-m-Y h:i:s", $update_timestamp);
print "last updated: $update_time";
}
else {
print "The file $filename does not exist";
}
?>
This will display something like:
last updated: 13-08-2002 06:16:19
Since the modify time is returned as a timestamp,
you need to convert it to date format. Other similar
functions are:
- int filectime ( string filename)
Returns the time the content or metadata (permissions etc) of a
file were changed.
- int filemtime(string filename)
Returns the time the file was last accessed.
- string filetype ( string filename)
Returns the file type (and FALSE if an error occurs. File type
can be one of: file (ordinary file), dir (a directory),
link (a symbolic link), block
(a block special device), char (a character special device),
fifo (FIFO, or a named pipe), or unknown (the type
cannot be determined
- int filegroup ( string filename)
Returns the group owner ID of the file (only in Unix). You can use
the function posix_getgrgid(), which returns an associative
array with group details, to get a name from the returned id
(using the name key), for example:
<?
$filename = "group.php";
$group_id = filegroup($filename);
$group = posix_getgrgid($group_id);
print "The file $group_id is owned by the group ".$group['name'].
"($group_id)";
?>
- int fileowner ( string filename)
Similar to filegroup, this returns the owner ID of the file
(only in Unix). You can use the function posix_getpwuid(),
which returns an associative array with owner details, to get a
name from the returned id (using the name key), for example:
<?
$filename = "owner.php";
$owner_id = fileowner($filename);
$owner = posix_getpwuid($owner_id);
print "The file $owner_id is owned by the owner ".$owner['name'].
"($owner_id)";
?>
- int fileperms ( string filename)
Returns the file permissions as a decimal number.
- bool is_dir(string filename)
Returns TRUE if the file is a directory, otherwise it returns FALSE
- bool is_file(string filename)
Returns TRUE if the file is an ordinary file, otherwise it returns FALSE
- bool is_link(string filename)
Returns TRUE if the file is a symbolic link, otherwise it returns FALSE
- bool is_executable(string filename)
Returns TRUE if the file is executable, otherwise it returns FALSE
- bool is_readable(string filename)
Returns TRUE if the file is readable, otherwise it returns FALSE
- bool is_writable(string filename)
Returns TRUE if the file is writable, otherwise it returns FALSE
An important fact about the above functions is that their results
are cached, since accessing the information can be fairly
resource-intensive. If any of the information is likely to change
during the request, you'll need to call the clearstatcache()
function to clear the cache, and get the data directly from the file:
void clearstatcache ( void)
Because it takes no parameters, simply call it as:
clearstatcache();
inside your script.
Copying and deleting files
You can always use the operating system commands to move, delete
and rename files, but that just makes your code less portable and
the PHP functions to do the same are easy. To copy a file, the
aptly named copy() function is used.
bool copy(string source_file,string destination_file)
The function returns TRUE if the file was correctly copied, or
FLASE if there was an error. Rename and delete are done in the
same way:
bool rename(string old_file,string new_file)
bool unlink(string filename)
Both these functions also return TRUE upon successful completion,
or FALSE if the operation did no complete successfully for some
reason.
Rolling over log files with copy, rename and unlink
Let's put this all together, and write a simple script that rolls
over logs. This script runs at the end of each day, and is
assumed to run inside a backup logs directory. The file
log3.txt is deleted, logs2.txt becomes logs3.txt,
and logs.txt becomes logs2.txt. The file binary_log
from the $logpath directory is copied to logs.txt.
<?
$binary_log = "$logpath/binary_log";
$logname = "logs.txt";
$logname2 = "logs_old.txt";
$logname3 = "logs_old2.txt";
if (!(unlink($logname3))) {
print "A problem with $logname3 - logs not rolled over";
exit;
}
if (!(rename($logname2,$logname3))) {
print "A problem with $logname2 - logs not rolled over";
exit;
}
if (!(rename($logname,$logname2))) {
print "A problem with $logname - logs not rolled over";
exit;
}
if (!(copy($binary_log,$logname))) {
print "A problem with $binary_log - logs not rolled over";
exit;
}
?>
Displaying files (Cont.) - Page 3
Handling files with PHP4 - Part1
Navigating within Files - Page 5
|