加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php分享一个操作文件的类(包括创建文件、读文件和写文件)

发布时间:2020-05-25 22:25:32 所属栏目:PHP 来源:互联网
导读:本文章向大家分享一个php创建文件、读文件和写文件的php类,需要的朋友可以参考一下。

php创建文件、读文件和写文件,代码如下:

class cfile {

protected $thepath;

//Error messages in the form of constants for ease of use.

const FOUNDERROR = "Sorry,the file in question does not exist.";

const PERMERROR = "Sorry,you do not have the proper permissions on this file";

const OPENERROR = "Sorry,the file in question could not be opened.";

const CLOSEERROR = "Sorry,the file could not be closed.";

//The constructor function.

public function __construct (){

$num_args = func_num_args();

if($num_args > 0){

$args = func_get_args();

$this->thepath = $args[0];

}

}

//A function to open the file.

/* http://www.manongjc.com/article/1343.html */

private function openfile ($readorwrite){

//First,ensure the file exists.

try {

if (file_exists ($this->thepath)){

//Now,you need to see if you are reading or writing or both.

$proceed = false;

if ($readorwrite == "r"){

if (is_readable($this->thepath)){

$proceed = true;

}

} elseif ($readorwrite == "w"){

if (is_writable($this->thepath)){

$proceed = true;

}

} else {

if (is_readable($this->thepath) && is_writable($this->thepath)){

$proceed = true;

}

}

try {

if ($proceed){

//You can now attempt to open the file.

try {

if ($filepointer = fopen ($this->thepath,$readorwrite)){

return $filepointer;

} else {

throw new exception (self::OPENERROR);

return false;

}

} catch (exception $e) {

echo $e->getmessage();

}

} else {

throw new exception (self::PERMERROR);

}

} catch (exception $e) {

echo $e->getmessage();

}

} else {

throw new exception (self::FOUNDERROR);

}

} catch (exception $e) {

echo $e->getmessage();

}

}

//A function to close a file.

function closefile () {

try {

if (!fclose ($this->thepath)){

throw new exception (self::CLOSEERROR);

}

} catch (exception $e) {

echo $e->getmessage();

}

}

//A function to read a file,then return the results of the read in a string.

public function read () {

//First,attempt to open the file.

$filepointer = $this->openfile ("r");

//Now,return a string with the read data.

if ($filepointer != false){

//Then you can read the file.

return fread ($filepointer,filesize ($this->thepath));

}

//Lastly,close the file.

$this->closefile ();

}

//A function to write to a file.

public function write ($towrite) {

//First,attempt to open the file.

$filepointer = $this->openfile ("w");

//Now,return a string with the read data.

if ($filepointer != false){

//Then you can read the file.

return fwrite ($filepointer,$towrite);

}

//Lastly,close the file.

$this->closefile ();

}

//A function to append to a file.

public function append ($toappend) {

//First,attempt to open the file.

$filepointer = $this->openfile ("a");

//Now,$toappend);

}

//Lastly,close the file.

$this->closefile ();

}

//A function to set the path to a new file.

public function setpath ($newpath) {

$this->thepath = $newpath;

}

}

?>

//Include the class.

require_once ("file.class.inc.php");

//Then create a new instance of the class.

$myfile = new cfile ("data.txt");

//Now,let's try reading it.

echo $myfile->read();

//Then let's try writing to the file.

$myfile->write ("Hello World!");

//Then,let's try appending.

$myfile->append ("Hello Again!");

?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读