1. Requirements
Learn about PHP's actions on files
2. Example
Write a class that can manipulate files, including add, delete, check
<?PHPclassmyfile{ Public functionWrite_file ($string,$path,$mode= ' W+b ') { if(!$fp[Email protected]fopen($path,$mode)) { Echo"Unable to write to file"; Exit(); } if(Flock($fp,lock_ex)) { for($written=0,$length=strlen($string);$written<$length;$written+=$result) { if(($result=fwrite($fp,substr($string,$written)))===false) { Break; } } Flock($fp,Lock_un); } Else { Echo"Unable to get file lock"; Exit(); } fclose($fp); return true; } Public functionRead_file ($path) { if(!$fp[Email protected]fopen($path, ' R+b ')) { Echo"Could not open file"; Exit(); } if(Flock($fp,lock_sh)) { if(!$content=fread($fp,filesize($path))) { Echo"Unable to read file"; Exit(); } } Else { Echo"Unable to get file lock"; Exit(); } fclose($fp); return $content; } Public functionDelete_file ($path) { if(!file_exists($path)) { Echo"File does not exist"; return false; } @unlink($path); return true; }}$config=Array( ' String ' = ' abcdeaisdiiee ', ' path ' = =MD5(Microtime()).‘. Php);$obj=Newmyfile ();$obj->write_file (' Sufi ', ' due '. php ', ' a+b ');
Reference Document: http://www.php.net/manual/zh/function.fwrite.php
PHP File Class