<?php
/*
simpleQuizz made Quizzs simple
Copyright (C) 2008 Mathieu Lory <hide@address.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @package simpleQuizz
* @subpackage file
* @author Mathieu Lory <hide@address.com>
*
*/
class file
{
/**
* File to be processed
*
* @access private
* @var string
*/
private $sFile;
/**
* Class constructor
*
* @access public
* @param string $sFile Source file
*/
public function __construct($sFile='')
{
if ($sFile != '')
{
$this->sFile = $sFile;
}
}
/**
* Read all content from a file and return as a string
*
* @access public
* @return string
*/
public function Read()
{
if ($this->sFile!='')
{
try
{
if ($this->Exists())
{
if ($this->IsReadable())
return file_get_contents($this->sFile);
else
throw new FileException('can\'t read file "'.$this->sFile.'"');
}
else
{
throw new FileException('file "'.$this->sFile.'" does not exist');
}
}
catch (Exception $e)
{
Debug::Log($e,ERROR);
return false;
}
}
return '';
}
/**
* Write some content to a file
*
* @access public
* @param string $sContent
* @return boolean
*/
public function Write($sContent)
{
try
{
return file_put_contents($this->sFile,$sContent);
}
catch (Exception $e)
{
Debug::Log($e,ERROR);
return false;
}
}
/**
* Append some content to file
*
* @access public
* @param string $sContent
* @return boolean
*/
public function Append($sContent)
{
try
{
return file_put_contents($this->sFile,$sContent,FILE_APPEND);
}
catch (Exception $e)
{
Debug::Log($e,ERROR);
return false;
}
}
/**
* Check if the selected file is readable
*
* @access public
* @return boolean
*/
public function IsReadable()
{
return is_readable($this->sFile);
}
/**
* Check if the selected file is writable
*
* @access public
* @return boolean
*/
public function IsWritable()
{
return is_writable($this->sFile);
}
/**
* Check if the selected file is exists
*
* @access public
* @return boolean
*/
public function Exists()
{
return file_exists($this->sFile);
}
/**
* Return the file name
*
* @access public
* @return string
*/
public function Name()
{
$sExt = strrchr($this->sFile,'.');
if ($sExt != false)
{
return substr($this->sFile,0,-strlen($sExt));
}
return '';
}
/**
* Return the file ext
*
* @access public
* @return string
*/
public function FileExt()
{
return strtolower(end(explode('.',$this->sFile)));
}
/**
* Delete a file from server
*
* @access public
* @return bool
*/
public function Delete()
{
if ($this->Exists())
{
return unlink($this->sFile);
}
return true;
}
/**
* List files from a directory
*
* @access public
* @return array
*/
public function GetFiles($dir, $regexp="/*/")
{
$liste = array();
$dh = opendir($dir);
//print $dir;
while (($file = readdir($dh)) !== false)
{
if (ereg($regexp, $file, $match))
{
$id = $match[1];
array_push($liste, $id);
}
}
//print_r($liste);
//print($dir);
return $liste;
}
}
?>