Development FileSystem

From Cloudrexx Development Wiki
Jump to: navigation, search

Important: This article contains incomplete/false information

Introduction

Requirements (DRAFT)

In general, the files of the Contrexx installation must be owned by the same user as the PHP instance of the web-server is running under

Apache Module

If PHP is used as Apache Module (Apache-Handler), then the Contrexx files must be owned by the user under which the Apache Webserver is running. This would usually be www-data. However the common use case is, that you won't be able to change the owner of the Contrexx files to the appropriate user. I.e. on shared hosting platforms, where you usually upload your files by FTP, the owner of your files will be your FTP-account. In such cases, it is inevitable to activate the FTP-support of Contrexx.

FastCGI

  • If PHP is used through the FastCGI interface, then the Contrexx files must be owned by the user under which the Apache Webserver is running

IIS

  • Set write access manually

Purpose

  • Permission access is handled by Contrexx (no more need for setting file permissions manually)
    Do not set file permissions manually using chmod(), instead use \Cx\Lib\FileSystem\FileSystem::makeWritable()
  • Write access to files limited to the owner of the file (no more global write access!)

API

File

Create

# Create new (empty) file

# CASE 1 (static)
\Cx\Lib\FileSystem\FileSystem::touch($absolute_path_to_file);

# CASE 2 (object)
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_file);
    $objFile->touch();
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Delete

# Delete file
\Cx\Lib\FileSystem\FileSystem::delete_file($absolute_path_to_file);

Write

# Write data to file
try {
    # $absolute_path_to_file can point to a new or existing file
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_file);
    $objFile->write($data);
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Read

# Read data from file
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_file);
    $data = $objFile->getData();
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Permissions / Write Access

# Add write access to existing file

# CASE 1 (static)
\Cx\Lib\FileSystem\FileSystem::makeWritable($absolute_path_to_file_or_directory);

# CASE 2 (object)
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_file_or_directory);
    if ($objFile->makeWritable()) {
        echo 'file is writable';
    }
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Copy

# copy file
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_existing_file);
    $objFile->copy($absolute_path_of_new_file_location_incl_file_name);
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Move

# move file
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_existing_file);
    $objFile->move($absolute_path_of_new_file_location_incl_file_name, $overwrite_existing_file = false);
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}

Folder

Create

# Create new folder
\Cx\Lib\FileSystem\FileSystem::make_folder($absolute_path_to_folder);

Delete

# Delete folder
\Cx\Lib\FileSystem\FileSystem::delete_folder($absolute_path_to_directory, $recursive = true);

Permissions / Write Access

# Add write access to existing folder

# CASE 1 (static)
\Cx\Lib\FileSystem\FileSystem::makeWritable($absolute_path_to_file_or_directory);

# CASE 2 (object)
try {
    $objFile = new \Cx\Lib\FileSystem\File($absolute_path_to_file_or_directory);
    if ($objFile->makeWritable()) {
        echo 'file is writable';
    }
} catch (\Cx\Lib\FileSystem\FileSystemException $e) {
    \DBG::msg($e->getMessage());
}