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

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

文件操作类:

rush: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; } } ?>
rush:PHP;"> read(); //Then let's try writing to the file. $myfile->write ("Hello World!"); //Then,let's try appending. $myfile->append ("Hello Again!"); ?>

文件下载:

rush:PHP;">

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...