Class: File Handler

Reference on the Doorkeeper File Handler class.

1189 views

Edited: 2021-03-07 18:53

The File Handler should generally be used whenever you want to write or read files from the file system.

It has simple handling of concurrency via file locking. If two "users" happen to write to the same file at the same time, one will wait for a random amount of time, while periodically trying obtaining a lock.

The "random" amount of time is a way to deal with dead locks on high traffic sites, but it is not a perfect solution, since it does not yet handle race conditions.

Note. the file handler requires write access in order to support file locking. It will currently not work without having write access, which would also defeat the primary purpose of its use; you can use the native file_get_contents if you do not care about locking.

Methods

Note that methods may accept an array as parameter. The stated values for array parameters are default values. I.e. 4096 is the default value for the optional max_line_length.

simple_delete()

path = string (required)
Returns true on success, and throws an Exception on failure.

delete_files()

path = string (required)
Returns true on success, and throws an Exception on failure.

read_file_lines()

path = string (required)
start_line = int (0)
max_line_length = int (4096)
lines_to_read = int (false)
Returns the file content on success, throws an Exception on failure.

count_lines()

path = string (required)
start_line = int (0)
max_line_length = int (4096)
lines_to_read = int (false)
Returns the line count of the file on success, and throws an Exception on failure.

write_file()

path = string (required)
content = string
mode = string (w)
Returns true on success, and throws an Exception on failure.

create_directory()

path = string (required)
permissions = int (0775)
Returns true on success, and throws an Exception on failure.

obtain_lock()

File Pointer
Returns true on success, and throws an Exception on failure.

scan_dir()

path = string (required)
Returns an array of files and directories on success, and false failure.

http_stream_file()

path = string (required)
chunk_size = int (8192)
Throws an Exception on failure.

download_to_file()

url = string (required)
output_file_path = string (required)
method = string (GET)
post_data = string
request_headers = array ([])
max_line_length = int (1024)
Returns true on success, and throws an Exception on failure.

Examples

  1. Streaming Video in PHP

Dependencies

  1. superglobals
  2. file_types

Improvements

This file handler is under active development. If you can improve it, feel free to help out. It could use improvements in how it handles concurrency to better deal with race conditions. A queue system would probably work best.

Currently, there's a miniscule chance a user could end up waiting for "a long time" to access a file, or until the script times out. This is a known limitation that might affect high traffic sites. When I wrote this, I did not intend it to be used on high-traffic sites, but rather to deal with rare dead locks caused by users accessing a file simultaneously.

Links

  1. Doorkeeper/lib/file_handler/ - GitHub

Tell us what you think: