Skip to content

Instantly share code, notes, and snippets.

@raul782
Created May 6, 2012 03:48
Show Gist options
  • Save raul782/2609901 to your computer and use it in GitHub Desktop.
Save raul782/2609901 to your computer and use it in GitHub Desktop.
Util File Core
<?php
class Utils_File_Core
{
protected $filename = "";
protected $path = "";
protected $fullfilename ="";
const NEWLINE = 0x0A;
const CARRIAGE = 0x0D;
/*
* Pass name of the file
*/
public function __construct($filename = null, $path = null)
{
if (!is_null($filename) && !is_null($path))
{
$this->filename = $filename;
$this->path = $path;
$this->fullfilename = $path.$filename;
}
}
/*
* Get Methods
*/
public function getFilename(){
return $this->filename;
}
public function getPath(){
return $this->path;
}
public function getFullfilename(){
return $this->fullfilename;
}
// Set Methods
public function setPath($path){
if(!empty($path) && !is_null($path))
{
$this->path = $path;
$this->fullfilename = $this->path.$this->filename;
}
}
public function setFilename($filename){
if(!empty($filename) && !is_null($filename))
{
$this->filename = $filename;
$this->fullfilename = $this->path.$this->filename;
}
}
public function write($text, $mode="w+"){
if ($this->is__writable($this->path)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($this->fullfilename, $mode)){
echo "Cannot open file ($this->fullfilename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $text) === FALSE) {
echo "Cannot write to file ($this->fullfilename)";
exit;
}
$echo = "Success, wrote ($text) to file ($this->fullfilename)";
fclose($handle);
} else {
echo "The file $this->fullfilename is not writable";
}
}
/**
* Get Content of a Plain File
*/
public function getContents(){
return file_get_contents($this->fullfilename);
}
/*
* Get Content to Array
*/
public function getContents2Array($separator=","){
return file($this->fullfilename);
}
public function appendHeader($header)
{
$appendMode = "a+";
if(is_array($header))
{
$header = str_replace('|',',',str_replace(',',' ',implode('|',$line)));
}
$this->write($header.chr(self::NEWLINE));
}
//@TODO : Support depth > 2
public function appendContentFromArray($content,$dimension=1)
{
$appendMode = "a+";
//$this->write('IdVendor,Vendor,ordersQty'.chr(self::NEWLINE));
foreach($content as $key => $line)
{
if($dimension == 1)
{
$this->write($key.",".$line.chr(self::NEWLINE), $appendMode);
}
else if ($dimension == 2)
{
$text = $key.",".str_replace('|',',',str_replace(',',' ',implode('|',$line)));
$this->write($text.chr(self::NEWLINE),$appendMode);
}
else if ($dimension == 3 )
{
$text = $key.",,";
$this->write($text.chr(self::NEWLINE),$appendMode);
foreach($line as $counter => $data)
{
$text = $counter.",".str_replace('|',',',str_replace(',',' ',implode('|',$data)));
$this->write($text.chr(self::NEWLINE),$appendMode);
}
}
}
}
/* Check if Directory has written permission */
private function is__writable($path) {
if ($path{strlen($path)-1}=='/')
return $this->is__writable($path.uniqid(mt_rand()).'.tmp');
if (file_exists($path)) {
if (!($f = @fopen($path, 'r+')))
return false;
fclose($f);
return true;
}
if (!($f = @fopen($path, 'w')))
return false;
fclose($f);
unlink($path);
return true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment