The WebAbility® Network Developers - Documentation
WebAbility Site
\datasource\FileSource reference

I. Structure:


- namespace:
\datasource\

- Direct superclass:
\datasource\DataSource

- Direct known subclasses:
none

II. Description:


This is the basic class to access a stream file on hard disk or alternate sources as supported by file(), file_get_contents() and fopen().
The pointer to the file should be writable if you plan to write your data source, or you will have errors.

The pointer to the file is defined with 3 parts
- A fixed part, called the base path. The base path is untouchable, it just exists by itself.
- A variable part, called the path. The path can be created by the FileSource if it not exists on write access.
- The file name, in the full path. On write access the file should be available to be created if it not exists.
The contatenation of the three parts MUST BE a valid file path.

Examples of paths (in red: basepath, in bold: path):
Read only:
http://www.webability.info/index.php
ftp://ftp.mysite.com/somedir/somefile.txt
Read/Write:
/home/sites/appdir/data/cache/myfile.txt
c:/My Documents/data/cache/myfile.txt
ftp://user:pass@ftp.mysite.com/somedir/somefile.txt


The FileSource will NOT cache in memory the content of the file. It is a 1-level read/write/unlink access only.
You can read the content of the file as a simple string, or as an array of lines with the read parameter.

The creation of the path is optional, and will happen only if you call the method ->setCreateDir(true);
If you don't allow the creation of the path, the write access will fail if the path does not exists.

unlink will physically deletes the file if write access is granted.


III. Reference:


3.1. Contructor:


\datasource\FileSource($basepath, $path, $filename, $createdir)
Creates the instance.
  • $basepath: string, the fixed path to our file.
  • $path: string, the variable path to our file.
  • $filename: string, the name of our file.
  • $createdir: boolean, optional, set to true to authorize path creation. Default value: false.
If the file does not exists, the object will be invalid until the file is created.


3.2. Constants:

There is no constant into this class.


3.3. Attributes:


protected string $basepath
Is used to store the fixed path of the file to access.

protected string $path
Is used to store the variable path of the file to access.

protected string $filename
Is used to store the name of the file to access.

protected boolean $createdir
Is used to store the flag of directories creation.
Set to false by default.


3.4. Methods:


public boolean static isValid()
This method is used to know if the data into the FileSource is valid or not.
If the file does not exists on the origin, or is not read/accessible, the object is not valid. Otherwise it is valid.

public integer getTimeStamp()
This method is used to get the last write access to the file.
If the object is invalid, it will return null.
Note: The returned value is a unix timestamp that you can format to a human readable date with date().
Note: Not all the types of streams have a timestamp. Be carefull using the sources of datas.

public void setCreateDir($flag)
This method is used to set the ability to create the variable path of the file if it does not exists.
  • $flag: boolean, set to true to authorize path creation.

public string getPath()
This method is used to get the path of our file.

public string getFilename()
This method is used to get the name of our file.

public mixed read($asarray)
This method is used to read our file.
If the object is invalid (file not accesible), it will return null.
  • $asarray: Optional, boolean. If true, the file will be returned as an array of lines. If false, it will return the fil ein a single string. Default value: false.

public void write($data)
This method is used to write the data into our file.
  • $data: string, the data to write in the object.

public void unlink()
This method is used to physically deletes the file.
The path is not deleted.
The object becomes invalid.