PHP实现的文件操作类及文件下载功能示例
本文实例讲述了PHP实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下:
文件操作类:
<?php //Copyright2005,LeeBabin(lee@thecodeshoppe.com) //Thiscodemaybeusedandredistributedwithoutcharge //underthetermsoftheGNUGeneralPublic //Licenseversion2.0orlater--www.gnu.org //Subjecttotheretentionofthiscopyright //andGPLNoticeinallcopiesorderivedworks classcfile{ //Thepathtothefilewewishtoworkwith. protected$thepath; //Errormessagesintheformofconstantsforeaseofuse. constFOUNDERROR="Sorry,thefileinquestiondoesnotexist."; constPERMERROR="Sorry,youdonothavetheproperpermissionsonthisfile"; constOPENERROR="Sorry,thefileinquestioncouldnotbeopened."; constCLOSEERROR="Sorry,thefilecouldnotbeclosed."; //Theconstructorfunction. publicfunction__construct(){ $num_args=func_num_args(); if($num_args>0){ $args=func_get_args(); $this->thepath=$args[0]; } } //Afunctiontoopenthefile. privatefunctionopenfile($readorwrite){ //First,ensurethefileexists. try{ if(file_exists($this->thepath)){ //Now,weneedtoseeifwearereadingorwritingorboth. $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){ //Wecannowattempttoopenthefile. try{ if($filepointer=fopen($this->thepath,$readorwrite)){ return$filepointer; }else{ thrownewexception(self::OPENERROR); returnfalse; } }catch(exception$e){ echo$e->getmessage(); } }else{ thrownewexception(self::PERMERROR); } }catch(exception$e){ echo$e->getmessage(); } }else{ thrownewexception(self::FOUNDERROR); } }catch(exception$e){ echo$e->getmessage(); } } //Afunctiontocloseafile. functionclosefile(){ try{ if(!fclose($this->thepath)){ thrownewexception(self::CLOSEERROR); } }catch(exception$e){ echo$e->getmessage(); } } //Afunctiontoreadafile,thenreturntheresultsofthereadinastring. publicfunctionread(){ //First,attempttoopenthefile. $filepointer=$this->openfile("r"); //Now,returnastringwiththereaddata. if($filepointer!=false){ //Thenwecanreadthefile. returnfgets($filepointer); } //Lastly,closethefile. $this->closefile(); } //Afunctiontowritetoafile. publicfunctionwrite($towrite){ //First,attempttoopenthefile. $filepointer=$this->openfile("w"); //Now,returnastringwiththereaddata. if($filepointer!=false){ //Thenwecanreadthefile. returnfwrite($filepointer,$towrite); } //Lastly,closethefile. $this->closefile(); } //Afunctiontoappendtoafile. publicfunctionappend($toappend){ //First,attempttoopenthefile. $filepointer=$this->openfile("a"); //Now,returnastringwiththereaddata. if($filepointer!=false){ //Thenwecanreadthefile. returnfwrite($filepointer,$toappend); } //Lastly,closethefile. $this->closefile(); } //Afunctiontosetthepathtoanewfile. publicfunctionsetpath($newpath){ $this->thepath=$newpath; } } ?>
<?php $myfile=newcfile("test.txt"); //Now,let'stryreadingit. echo$myfile->read(); //Thenlet'strywritingtothefile. $myfile->write("HelloWorld!"); //Then,let'stryappending. $myfile->append("HelloAgain!"); ?>
文件下载:
<?php $filename='file1.txt'; $file=fopen($filename,'r'); Header("Expires:0"); Header("Pragma:public"); Header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); Header("Cache-Control:public"); Header("Content-Length:".filesize($filename)); Header("Content-Type:application/octet-stream"); Header("Content-Disposition:attachment;filename=".$filename); readfile($filename); ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。