sFire PHP Framework

File system: Working with Files

sFire\System\File lets you create and manipulate files. It can give you information about the size, permissions, owners and more in a handy interface.

In this section we will handle:

  • Create a new instance
  • Get the file entity
  • Create a file
  • Get the contents of the file
  • Move a file
  • Delete a file
  • Copy a file
  • Rename a file
  • Append, prepend and flush a file
  • Check if a file is readable or writable
  • Check if a file exists
  • Change file permissions
  • Retrieve and change file owner
  • Retrieve the access time and modification time
  • Sets access and modification time of file
  • Determine the file size, group, and mime type
  • Get global information about a file

Create a new instance

To create a new sFire\System\File instance, you call the constructor. The constructor requires a file path as a String. This does not have to be an existing file.

use sFire\System\File;

$dir = new File('foo.txt');

With this instance you can chain methods. For example, you can create, rename and move the file in one go:

new File('foo.txt') -> create() -> rename('bar.txt') -> move('new/location/');

Get the entity of the file

The given fileis stored in sFire\Entity\File which enables you to retrieve and set new data for the file.

$file = new File('foo.txt');
$entity = $file -> entity();

print_r($entity);

Create a file

To create a new file, you can use the create method. First create an instance with the file you want to create and then call the create method. In this example, the "foo.txt" file will be created in the current working directory.

$file = new File('foo.txt');
$file -> create();

Get the content of the file

To retrieve the content of a file, you can use the getContent method.

$file -> getContent(); //Returns the content as a String

Move a file

You can move a file to a new location. The new location should be an existing directory and this directory should be writable.

$file -> move('new/location/'); //Moves file to the "new/location" directory

Delete a file

To delete a file, you can use the delete method. This will remove the file permanent.

$file -> delete(); //Deletes the file permanent

Copy a file

To copy a file, you can use the copy method. This will copy the file to a new location. The new location should be an existing directory and should be writable.

$file -> move('new/location/'); //Moves file to the "new/location" directory

Rename a file

To rename a file, you can use the rename method. This will rename the file to a new given name.

$file -> rename('bar.txt'); //Renamed the file to "bar.txt"

Append, prepend and flush a file

To write new content to a file, you can use the append and prepand methods. If you want to clear the content of a file, you can use the flush method.

$file = new File('foo.txt');
$file -> append('4 5 6');
$file -> prepend('1 2 3 '); //Content will be "1 2 3 4 5 6"

$file -> flush(); //Content will be empty

Check if a file is readable or writable

To make sure you can write to a file, you should check if the given file is writable. You can use the isWritable method to return Boolean true if the file is writable or false if not.

if(true === $file -> isWritable()) {
    //File is writable
}

You can also check if a file is readable using the isReadable method. This method will return Boolean true if the file is readable or false if not.

if(true === $file -> isReadable()) {
    //File is readable
}

Check if a file exists

To make sure a given file exists, you can use the exists method to return a boolean true if exists or false if not

if(true === $file -> exists()) {
    //File exists
}

Change file permissions

To change the permissions of a file, you can call the chmod method. This method will return a boolean true on success or false on failure.

$file -> chmod(0600); //Read and write for owner, nothing for everybody else
$file -> chmod(0644); //Read and write for owner, read for everybody else
$file -> chmod(0755); //Everything for owner, read and execute for others
$file -> chmod(0750); //Everything for owner, read and execute for owner's group

Note: Note that mode is not automatically assumed to be an octal value, so to ensure the expected operation, you need to prefix mode with a zero (0). Strings such as "g+w" will not work properly.

Retrieve and change file owner

To change the owner of a file, you can call the chown method. This method will return a Boolean true on success or false on failure.

$file -> chown('user'); //Read and write for owner, nothing for everybody else

Note: Only the superuser may change the owner of a file.

To retrieve the file owner, you can use the getOwner method.

$file -> getOwner(); //Output similar to "root"

Retrieve the access time and modification time

To retrieve the access and modification time, you can use the getAccessTime and getModificationTime methods. Both will return a Unix timestamp if the file exists.

$file = new File('foo.txt');
$file -> getAccessTime(); //Output similar to 1509952240
$file -> getModificationTime(); //Output similar to 1509952290

Sets access and modification time of file

You can set a new access time and modification time for a file using the touch method. This method will accept two Integer arguments with a Unix timestamp as a valid value (which can be created with the time() method of PHP). If the file does not exist, it will be created.

$accesstime         = 1509952240;
$modificationtime   = 1509952290;

$file = new File('foo.txt');
$file -> touch($accesstime, $modificationtime);

Determine the file size, group, and mime type

To retrieve the file size and group name, you can use the getFilesize and getGroup methods. The getFilesize method will return the filesize in bytes of current file while the getGroup method returns the current group as a String.

$file = new File('foo.txt');
$file -> getFilesize(); //Output similar to 546382
$file -> getGroup(); //Output similar to "root"

The Mime type is determined on a file extension. Currently more than 440 extensions are documented which can be found in sFire\System\Mime.

$file = new File('foo.txt');
$file -> getMime(); //Output similar to: "text/plain"

Get global information about a file

The info method will return an Array with global information about a file.

$file = new File('foo.txt');
$info = $file -> info();
print_r($info); 

//Output similar to:
Array
(
    [name] => foo
    [basename] => foo.txt
    [basepath] => /var/www/foo.txt
    [extension] => txt
    [exists] => 1
    [filesize] => 18
    [accesstime] => 1509953104
    [path] => /var/www/
    [group] => 1000
    [modificationtime] => 1509953060
    [readable] => 1
    [mime] => 
    [owner] => 1000
    [writable] => 1
    [width] => 
    [height] => 
)