File: FileAccess.php

Recommend this page to a friend!
  Classes of Afif Ahmad Hidayat  >  File Access  >  FileAccess.php  >  Download  
File: FileAccess.php
Role: Class source
Content type: text/plain
Description: Main class
Class: File Access
Perform basic file manipulation operations
Author: By
Last change: remove example
Date: 5 years ago
Size: 2,589 bytes
 

 

Contents

Class file image Download
<?php

class FileAccess {

    private
$root;
    private
$newcontent;
    private
$file;
    protected static
$instance;
    private
$default_path = true;

    public static function
getInstance(){
        if(
is_null(self::$instance)){
           
self::$instance = new self();
        }
        return
self::$instance;
    }
   
    protected function
__construct(){
    }
       
    public function
set($file){
       
$file = preg_replace('#[\\\\/]+#', DIRECTORY_SEPARATOR, $file);
       
$this -> file = $file;
        return
$this;
    }

    public function
get(){
        if(
$this -> file){
            return
$this -> file;
        }
        return
null;
    }

    public function
save($string){
        if(
$this -> validateDirectory()){
           
file_put_contents($this -> get(),$string);
            return
$this;
        }
    }
   
    public function
saveAppend($string){
        if(
$this -> validateDirectory()){
           
file_put_contents($this -> get(),$string, FILE_APPEND);
            return
$this;
        }
    }

    public function
savePrepend($string){
        if(
$this -> validateDirectory()){
           
$handle = fopen($this -> get(),'a+');
           
fclose($handle);
           
$handle = fopen($this -> get(),'r+');
           
$len = strlen($string);
           
$final_len = filesize($this -> get()) + $len;
           
$cache_old = fread($handle, $len);
           
rewind($handle);
           
$i = 1;
            while (
ftell($handle) < $final_len) {
               
fwrite($handle, $string);
               
$string = $cache_old;
               
$cache_old = fread($handle, $len);
               
fseek($handle, $i * $len);
               
$i++;
            }
           
fclose($handle);
        }
        return
$this;
    }

    public function
exists(){
       
$args = func_get_args();
        if(!
$args){
            return
file_exists($this -> get());
        } else {
            return
file_exists($args[0]);
        }
    }

    protected function
validateDirectory(){
       
$args = func_get_args();
        if(!
$args){
           
$directory = dirname ($this -> get());
        } else {
           
$directory = dirname ($args[0]);
        }
        if(!
is_dir($directory)){
            if(
file_exists($directory)){
                return
false;
            }
           
$this -> validateDirectory($directory);
            if(
is_writable(dirname($directory))){
               
mkdir($directory);
            } else {
                return
false;
            }
        }
        return
true;
    }
   
    public function
render(){
        if(
$this -> exists()){
            return
file_get_contents($this -> get());
        }
        return
false;
    }
   
    public function
delete(){
        if(
$this -> exists()){
            return @
unlink($this -> get());
        }
        return
false;
    }
   
    public function
modified(){
        if(
$this -> exists()){
            return
filemtime($this -> get());
        }
        return
false;
    }
   
    public function
getAge(){
        if((
$mod = $this -> modified())){
            return
$_SERVER["REQUEST_TIME"] - $mod;
        }
        return
false;
    }
}


For more information send a message to info at phpclasses dot org.