File/src/file.php

Description

File management

Functions to read, write and modify files in various ways. All the functions do their own locking and error checking. In most cases a read or write error will be fatal and produce a nice error-description for the user in the browser and logs.

To work with text files: FileRead(Raw) FileWrite(Raw) To work with bash shell compatible config files: FileReadSetting(s)/FileWriteSetting(s) To store php array structures in textformat: FileExport/FileImport.

Includes
 require_once ("common.php") (line 15)
Constants
ADDED = 1 (line 23)
CHANGED = 3 (line 25)
DELETED = 2 (line 24)
SAME = 0 (line 22)

Constants for the FileReplaceLine() function.

Functions
DetermineLockfile (line 30)

determine appropriate lockfilename for a file.

void DetermineLockfile ( $path)
  • $path
DetermineOldfile (line 40)

determines the name of a old backup file.

void DetermineOldfile ( $path)
  • $path
DownloadProgress (line 618)

Downloads $srcurl to $targetpath with progress box and error checking.

Use this if you need to download large stuff from the internet. Also used in the update manager module. Specify $size to give the user an indication of the completion status.

void DownloadProgress ( $srcurl,  $size,  $targetpath)
  • $srcurl
  • $size
  • $targetpath
FileChgrp (line 536)

Chgrp a file, catches errors

void FileChgrp ( $file,  $group)
  • $file
  • $group
FileChmod (line 545)

Chmod a file, catches errors

void FileChmod ( $file,  $mode)
  • $file
  • $mode
FileChown (line 527)

Chown a file, catches errors

void FileChown ( $file,  $user)
  • $file
  • $user
FileCopy (line 413)

Copy a file.

void FileCopy ( $patha,  $pathb)
  • $patha
  • $pathb
FileDelete (line 392)

Delete a file.

Calls Error() on failure.

void FileDelete ( $path)
  • $path
FileExport (line 677)

Exports the php object to a file.

The object can contain any type and any amount of subarrays. Use this to 'serialize' the data in a human readable/editable way. TODO: we zouden hiervoor json->encode moeten gebruiken, maar deze kan (nog) geen mooie human readable output genereren

void FileExport ( $path,  $object)
  • $path
  • $object
FileImport (line 688)

Imports a php object from a file (see FileExport)

Caution! Make sure you trust the inputfile, since it will be parsed as php code!

void FileImport ( $path)
  • $path
FileLock (line 49)

lock the filename, by creating a lockfile in workdir. returns a pointer to the lockfile on success.

(false otherwise)

void FileLock ( $path)
  • $path
FileMkdir (line 554)

Mkdir, catches errors.

Also creates higher paths.

void FileMkdir ( $path, [ $mode = '0775'])
  • $path
  • $mode
FileMove (line 406)

Moves a file.

void FileMove ( $patha,  $pathb)
  • $patha
  • $pathb
FileRead (line 91)

reads a textfile safely by locking it first

Errors are fatal and will call the Error() function.

  • return: An array that contains a row for each line in the file.
array FileRead (string $path, [int $locking = 1], [int $hashed = 0])
  • string $path: The pathname of the file
  • int $locking: Make this 0 to disable locking
  • int $hashed: Make this 1 to return a hasharray instead of a normal array. Each line becomes a key. Duplicate lines will be removed this way.
FileReadMatches (line 489)

return array with the lines in the file that match the regex.

See preg_match() for more info.

void FileReadMatches ( $path,  $searchregex, [int $invert = 0])
  • int $invert: to inverse functionality If $invert==1, the lines that not match will be returned
  • $path
  • $searchregex
FileReadRaw (line 133)

reads a raw file safely by locking it first.

NOTE: doesn't work on pseudo files in /proc ! Errors are fatal and will call the Error() function.

  • return: The raw data of the file, including newlines and binary stuff.
string FileReadRaw (string $path, [int $locking = 1])
  • string $path: The pathname of the file
  • int $locking: Make this 0 to disable locking
FileReadSetting (line 339)

Reads one specific setting

The format of a settings files is parsable by shellscripts and has the following format: settingname="settingsvalue"

  • return: settingvalue
string FileReadSetting (string $path, string $varname)
  • string $path: The path of the file to load
  • string $varname: The name of the setting
FileReadSettings (line 313)

Reads a settings file into a hash array

The format of a settings files is parsable by shellscripts and has the following format: settingname="settingsvalue"

  • return: ret[settingname]=settingvalue;
hasharray FileReadSettings (string $path)
  • string $path: The path of the file to load
FileReplaceLine (line 433)

Replaces a specific line in a file.

If $searchregex is not found in the file it will just add the line to the file. If $newdata is not specified (false) it will delete the line instead of replacing it. NOTE: Doesn't use locking between the FileRead and FileWrite yet.

  • return: Returns one of these predifined constants, depending on the action that was taken: SAME, ADDED, DELETED, CHANGED.
int FileReplaceLine (string $path, string $searchregex, [string $newdata = false], [int $pregmode = 1])
  • string $path: The path of the file to write
  • string $searchregex: PHP/perl regular expression. See preg_match on php.net for more info about how to use these. You should learn this! :)
  • string $newdata: The new data to replace the matched line with.
  • int $pregmode: Set this to 0 to use simple strstr() matching instead. Only use this for performance reasons, and not because you're to lazy to learn preg_match().
FileRmdir (line 564)

Rmdir, catches errors.

Recursively

void FileRmdir ( $path)
  • $path
FileSymlinkReplace (line 515)

create a new symlink, replacing any existing files.

void FileSymlinkReplace ( $target,  $link)
  • $target
  • $link
FileUnlock (line 67)

unlock the specified file/filepointer combination.

removes the lockfile

void FileUnlock ( $path,  $fp)
  • $path
  • $fp
FileWrite (line 200)

write a TEXT file safely by locking it and making a copy of the old file in basedir

Errors are fatal and will call the Error() function.

  • return: True on success
boolean FileWrite (string $path, array $data, [int $hashed = 0])
  • string $path: The pathname of the file
  • array $data: The data in array or hasharray format
  • int $hashed: Make this 1 to use a hasharray instead of a normal array.
FileWriteRaw (line 258)

write a RAW file safely by locking it and making a copy of the old file in basedir

Errors are fatal and will call the Error() function.

  • return: True on success
boolean FileWriteRaw (string $path, string $rawdata)
  • string $path: The pathname of the file
  • string $rawdata: The raw datablock, including newlines and binary stuff.
FileWriteSetting (line 376)

Write one specific setting to a settings file

The format of a settings files is parsable by shellscripts and has the following format: settingname="settingsvalue"

  • return: Returncode of the filewrite function
int FileWriteSetting (string $path, string $varname, string $value)
  • string $path: The path of the file to write
  • string $varname: The name of the setting
  • string $value: The value of the setting
FileWriteSettings (line 356)

Write a hash array with settings to a settings file

The format of a settings files is parsable by shellscripts and has the following format: settingname="settingsvalue"

  • return: Returncode of the filewrite function
int FileWriteSettings (string $path, string $data)
  • string $path: The path of the file to write
  • string $data: The settings hasharray $data[settingsname]=settingsvalue;
RemoveExtension (line 599)

Removes the data after the last dot from a filename. 'package.tgz' is returned as 'package'.

NOTE: 'package.tar.gz' is returned as 'package.tar'

void RemoveExtension ( $strName)
  • $strName
scandir (line 578)

Returns the files in the directory $start_directory

void scandir ( $dir, [int $recursive = 0], string $start_directory)
  • string $start_directory: the base directory it should scan from.
  • int $recursive: Set this to 1 to use scan recursive.
  • $dir

Documentation generated on Tue, 06 Jan 2009 12:38:13 +0100 by phpDocumentor 1.4.1