class File
{
private $rootPath ;
private $content ;
private $fileName;
private $fileContent;
private $fileHandle;
private $contentDirFile = array();
private $dirSeparator = '/';
public function __construct( )
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned on');
}
/**
*
* @return unknown_type
*/
public function getPath ( $rootPath )
{
$this->rootPath = $rootPath ;
$this->content = $this -> createContent ($this -> rootPath );
}
public function __destruct()
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned off');
}
public function __get($mod)
{
return Mp::getInstance()->getMod($mod);
}
/**
* method creating array containing the directories and files
* @param String $dir contain rootPath name
* @return Array return array containing the directories and files
*/
private function createContent( $dir )
{
$dir = rtrim ($dir, '/');
if (is_dir ($dir)) {
$dh = opendir ($dir);
} else
{
echo $dir, ' n\'est pas un repertoire valide';
exit;
}
while (($file = readdir ($dh)) !== false ) {
if ($file !== '.' && $file !== '..') {
$rootPath =$dir.'/'.$file;
if (is_dir ($rootPath)) {
$this->contentDirFile[$dir]['dir'][] = $rootPath;
$tabTmp = $this -> createContent ($rootPath);
if (is_array ($tabTmp) && is_array ($this->contentDirFile))
$this->contentDirFile = array_merge ($this->contentDirFile, $tabTmp);
}
else
$this->contentDirFile[$dir]['file'][] = $rootPath;
}
}
closedir ($dh); // on ferme le repertoire courant
return $this->contentDirFile;
}
/**
* method creating array containing the directories
* @return Array return array directories
*/
public function recursiveDisplayDir( )
{
if ( is_array( $this->content[$this -> rootPath]['dir'] )){
return $this->content[$this -> rootPath]['dir'];
}
}
/**
* method creating array containing files
* @return Array return array files
*/
public function recursiveDisplayFile ( )
{
if (is_array ($this->content[$this -> rootPath]['file'])) {
return $this->content[$this -> rootPath]['file'];
}
}
/**
* method creating a directory
* @param String $dirName contain directory name
* @param Int $mode contain mode permission
* @return bool return true on success
*/
public function createDirectory( $dirName , $mode = 0755 )
{
$slash = '/';
(stristr( $this->rootPath, $slash )) ? '' : $slash = '\\';
if ( !empty($dirName) && !is_dir($dirName) ){
$dirPath = $this->rootPath.$slash.$dirName;
if( mkdir ( $dirPath, $mode )){
return true ;
} else {
return false ;
}
}
}
/**
* method deleting a directory
* @param String $dirName contain directory name
* @return bool return true on success
*/
public function DeleteDirectory( $dirName )
{
$dirPath = $this->rootPath.$this->dirSeparator.$dirName.$this->dirSeparator ;
if ( is_dir ( $dirPath )){
$fileList = glob( $dirPath . '*', GLOB_MARK );
foreach( $fileList as $file ){
if( substr( $file, -1 ) == '/' ){
$this->DeleteDirectory( $file );
}else{
unlink( $file );
}
}
rmdir( $dirPath );
}
}
/**
* method creaing in file
* @param String $fileName contain a resouce
* @param String $mode contain a mode permission
* @return bool return true on success
*/
public function createFile( $fileName , $mode)
{
$this->fileName = $this->rootPath.$this->dirSeparator.$fileName;
$this->fileHandle = fopen( $this->fileName, "$mode");
if ( $this->fileHandle ){
return true ;
}else{
return false ;
}
}
/**
* method reading in file
* @param String $fileName contain a resouce
* @return String return file content
*/
public function readFile( $fileName )
{
$fileName = $this->rootPath.$this->dirSeparator.$fileName;
if ( file_exists ( $fileName ) && is_readable( $fileName ) ){
$this->fileHandle = fopen( $fileName , "r" );
$this->fileContent = fread( $this->fileHandle , filesize($fileName) );
$this->closeFile( $this->fileHandle );
}
return $this->fileContent ;
}
/**
* method writing in file
* @param String $fileName contain a resouce
* @param String $fileContent contain a resouce
* @return bool return true on success
*/
public function writeFile( $fileName, $fileContent )
{
$file = $this->rootPath.$this->dirSeparator.$fileName;
if ( file_exists ( $file ) && is_writable( $file ) ){
$handle = fopen( "$file" , "a" );
fwrite( $handle, $fileContent );
$this->closeFile( $handle );
}else{
return false ;
}
}
/**
* method deleting a directory
* @param String $fileHandle contain a resouce
* @return bool return true on success
*/
public function closeFile( $fileHandle )
{
fclose( $fileHandle );
}
}
/******************* utlisation ********************************/
<?php
$te = dirname(__FILE__) ; // pour creation fichier
$test = new File();
$test->getPath ( $te );
print"<pre>";
//print_r( $test->recursiveDisplayFile() );
//print_r( $test->recursiveDisplayDir());
//print_r( $test->createFile("tes222t.php","a+") );
//print_r( $test->DeleteDirectory("test2") );
//print_r( $test->writeFile("tes222t.php","testetttfgtuhftt") );
//print_r( $test->readFile("tes222t.php") );
print_r( $test->createDirectory("test2", 0700) );
?>
Tags : dossier recursive php fichier file system php
» Catégorie
divers
Commentaires (3)
::
Poster un commentaire
::
Lien permanent ::
Envoyer à un ami
requete HTTP et reponse avec curl en php
: Ajouté le 18/8/2009 à 15:45
class Curl
{
private $handleCurl;
private $lastHeader;
private $httpCode;
public function __construct()
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned on');
}
public function __destruct()
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned off');
}
public function __get($mod)
{
return Mp::getInstance()->getMod($mod);
}
/**
* retrieve a curl request by get
* @param String $url contain a url
* @param String $user contain user login
* @param String $pass contain user password
* @param Array $option contain other
* @return Array retuen array curl
*/
public function getUrl( $url , $user = null , $pass = null, $option = array() )
{
$this->handleCurl = curl_init() ;
$curlOption = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
CURLOPT_TIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERPWD => $user.":".$pass
);
curl_setopt_array($this->handleCurl, $curlOption );
if ( is_array( $option )){
foreach ($option as $opt => $value) {
curl_setopt($this->handleCurl, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($opt))), $value);
}
}
if (curl_exec ( $this->handleCurl ))
{
$this->lastHeader = curl_getinfo ( $this->handleCurl , CURLOPT_HEADER );;
$this->httpCode = curl_getinfo($this->handleCurl , CURLINFO_HTTP_CODE);
return curl_getinfo ( $this->handleCurl );
}
}
/**
* retrieve a curl request by post
* @param String $url contain a url
* @param String $user contain user login
* @param String $pass contain user password
* @param Array $option contain other
* @param Array $data contain content to post
* @return Array retuen array curl
*
*/
public function PostUrl( $url , $data , $user = null , $pass = null, $option = array() )
{
$this->handleCurl = curl_init() ;
$curlOption = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-type: text/xml'),
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERPWD => $user.":".$pass
);
curl_setopt_array($this->handleCurl, $curlOption );
if ( is_array( $option )){
foreach ($option as $opt => $value) {
curl_setopt($this->handleCurl, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($opt))), $value);
}
}
if (curl_exec ( $this->handleCurl ))
{
$this->lastHeader = curl_getinfo ( $this->handleCurl , CURLOPT_HEADER );
$this->httpCode = curl_getinfo($this->handleCurl , CURLINFO_HTTP_CODE);
return curl_getinfo ( $this->handleCurl );
}
}
/**
*
* @return Array return last header if http code equal 200
*/
public function getLastHeader()
{
if( $this->httpCode != 200 ){
$this->getLastError( $this->handleCurl );
}else{
return $this->lastHeader ;
}
}
/**
* retrieve errors
* @param Bool $logError contain result handle cur_ini
* @return bool return result error
*/
public function getLastError( $logError = false )
{
if( $logError ) {
$curls_errors['num'] = curl_errno( $logError );
$curls_errors['msg'] = curl_error( $logError );
$this->Log->add(__METHOD__, __CLASS__ . $curls_errors['msg'] );
curl_close( $logError );
return false;
}
}
}
Tags : curl php http request
» Catégorie
divers
Commentaires (0)
::
Poster un commentaire
::
Lien permanent ::
Envoyer à un ami
class config (.ini) de votre application et stockage en cache en php
: Ajouté le 18/8/2009 à 15:39
class Config
{
private $_file_ini = "";
private $_file_ser = "";
private $content = array();
/**
* return a config for a site and load config file
* @param String $file path of the config file
* @return an objet
*/
public function __construct( $file )
{
if(file_exists($file)) {
$this->_file_ini = $file ;
$this->_file_ser = $this->_file_ini .'.php';
$this->getCache( );
}
}
/**
* return configuration value
* @param String $sectionName The section will contain the parameter name
* @param String $paramName will contani the parameter name
* @return the parameter value
*/
public function getParameterByName( $sectionName=null, $paramName=null )
{
if(is_array($this->content)) {
if($sectionName !== null && isset($this->content[ $sectionName ])) {
if($paramName !== null && isset($this->content[ $sectionName ][ $paramName ])) {
return $this->content[ $sectionName ][ $paramName ];
} elseif ($paramName !== null && !isset($this->content[ $sectionName ][ $paramName ])) {
} else {
return $this->content[ $sectionName ];
}
} elseif ($sectionName !== null && !isset($this->content[ $sectionName ])) {
} else {
return $this->content;
}
}
return null;
}
/**
* return informations in cache
* @return bool true for successful loading config or false
*/
private function getCache( )
{
if ( file_exists( $this->_file_ser )){
$unserializeContent = file_get_contents( $this->_file_ser );
// var_dump( $unserializeContent );
if( $unserializeContent ) {
$unserializeContent = str_replace( '<?php return false; ?>', '', $unserializeContent );
$result = unserialize ( $unserializeContent );
if(is_array($result) && isset($result['md5_file'])) {
if( $result['md5_file'] == Md5_file( $this->_file_ini ) ) {
$this->content = $result;
return true;
}
}
}
}
return $this->setCache();
}
/**
* create cache of ini file
* @return bool true for successful loading config or false
*/
private function setCache( )
{
$this->content = parse_ini_file( $this->_file_ini, true );
$this->content['md5_file'] = Md5_file($this->_file_ini);
$serializeContent = '<?php return false; ?>'. serialize ( $this->content );
file_put_contents ( $this->_file_ser, $serializeContent );
return true;
}
}
/*********************************** utilisisation *****************************************************/
config.ini
#<?php return false; ?>
[General]
url = "http://www.example.com"
host = localhost
username = user
password = password
db = cms
adapter = mysql
[Path]
root = "/www/root/"
/*** test ***/
<?php
require_once ("config.php");
$test = new Config("config.ini");
print '<pre> Tableau des valeur de général : '; print_r( $test-> getParameterByName("General") ); print '</pre>';
?>
Tags : cache php fichier ini php class confi
» Catégorie
divers
Commentaires (0)
::
Poster un commentaire
::
Lien permanent ::
Envoyer à un ami