<?php
/**
* Easy going file access. Obligatory Layer.
* @author Axel Benz
* @package file_system_access
*
*/
//require_once(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
// "../error_handling/error_handling.php");
class easy_file{
const invalid_characters = "^°\"§\$%&/()[]{}=?`´ß@*+~'#<>|,;:äöüÄÖÜ ";
/**
* Write a file, overwrite if it is already there
*
* @param string $filename
* The path to the file to write and the filename.
* Must NOT be an URL, but a file system path!
* @param string $content
* The content to write
*/
public static function write($filename, $content){
error_reporting(E_ERROR);
$handle=fopen($filename,"wb");
fwrite($handle,$content);
fclose($handle);
error_reporting(E_ALL);
}
/**
* Test whether a file exists
*
* @param string $filename
* The path to the file and the filename
* @return boolean
* True, iff file exists.
*/
public static function exists($filename){
clearstatcache();
return file_exists($filename);
}
/**
* Test, whether something is a file
*
* @param string $path
* The path to the file to test.
* @return boolean
* True, if it is a regular file.
*/
public static function is_file($path){
clearstatcache();
return is_file($path);
}
/**
* Test, whether something is a directory
*
* @param string $path
* The path to test.
* @return boolean
* True, if it is a directory.
*/
public static function is_dir($path){
clearstatcache();
return is_dir($path);
}
/**
* Read a whole file into a string.
*
* @param string $filename
* The path to the file and the filename
* @return string
* The content of the file
*/
public static function read($filename){
return file_get_contents($filename);
}
/**
* Read a whole file into a array of strings (linewise).
*
* @param string $filename
* @return string[]
*/
public static function read_lines($filename){
return explode("\n",self::read($filename));
}
/**
* Delete a file
*
* @param string $filename
* The path to the file and the filename
*/
public static function delete($filename){
return unlink($filename);
}
/**
* Copy a file
*
* @param string $old_file
* Filename an path of the file to copy
* @param string $new_file
* Filename and path of the file to create
*/
public static function copy($old_file,$new_file){
$res = copy($old_file, $new_file);
}
/**
* Validates a filename
*
* @param string $file
* The filename to validate
*
* @return boolean
* true, if filename is valid
*/
public static function is_filename($file){
foreach (str_split(self::invalid_characters) as $chr){
if (!(strpos($file,$chr)===false)){
return false;
}
}
return true;
}
/**
* Removes ".." and "." from any given path.
* Works also with urls.
*
* @param string $path
*/
public static function clean_path($path){
$path=str_replace("\\","/",$path);
$path_ar = explode("/",$path);
#remove the "."
foreach ($path_ar as $i=>$pathelement){
if ($pathelement=="."){
unset($path_ar[$i]);
}
}
#remove the ".."
foreach ($path_ar as $i=>$pathelement){
if ($pathelement==".."){
unset($path_ar[$i]);
$j=$i-1;
while (! isset($path_ar[$j]) and $j>0){
$j--;
}
unset($path_ar[$j]);
}
}
return implode("/",$path_ar);
}
}
?>