加入收藏 | 设为首页 | 会员中心 | 我要投稿 甘南站长网 (https://www.0941zz.com/)- 科技、行业物联网、开发、云计算、云管理!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP实现的文件操作类及文件下载功能示例

发布时间:2021-01-11 14:06:08 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了PHP实现的文件操作类及文件下载功能,结合实例形式分析了php针对文件的读、写、创建及下载等功能实现技巧,需要的朋友可以参考下

本文实例讲述了PHP实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下:

文件操作类:

0){ $args = func_get_args(); $this->thepath = $args[0]; } } //A function to open the file. private function openfile ($readorwrite){ //First,ensure the file exists. try { if (file_exists ($this->thepath)){ //Now,we need to see if we 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){ //We 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 we can read the file. return fgets ($filepointer); } //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 we 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; } } ?>
read(); //Then let's try writing to the file. $myfile->write ("Hello World!"); //Then,let's try appending. $myfile->append ("Hello Again!"); ?>

文件下载:

(编辑:甘南站长网)

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

    推荐文章
      热点阅读