国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP實(shí)現(xiàn)的文件操作類(lèi)及文件下載功能示例

2024-05-04 23:53:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了PHP實(shí)現(xiàn)的文件操作類(lèi)及文件下載功能。分享給大家供大家參考,具體如下:

文件操作類(lèi):

<?php // Copyright 2005, Lee Babin (lee@thecodeshoppe.com) // This code may be used and redistributed without charge // under the terms of the GNU General Public // License version 2.0 or later -- www.gnu.org // Subject to the retention of this copyright // and GPL Notice in all copies or derived works class cfile {  //The path to the file we wish to work with.  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.  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, return a string with the read data.    if ($filepointer != false){      //Then we can read the file.      return fwrite ($filepointer, $toappend);    }    //Lastly, close the file.    $this->closefile ();  }  //A function to set the path to a new file.  public function setpath ($newpath) {    $this->thepath = $newpath;  } }?>
<?php  $myfile = new cfile ("test.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!");?>

文件下載:

<?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);?>

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 南充市| 海门市| 富蕴县| 浙江省| 石河子市| 临夏县| 巫山县| 芦溪县| 仁寿县| 将乐县| 湖州市| 马龙县| 孟连| 札达县| 吴川市| 松溪县| 天全县| 项城市| 太康县| 二手房| 隆昌县| 宁蒗| 裕民县| 蕉岭县| 澎湖县| 东源县| 丹巴县| 玉门市| 团风县| 永宁县| 鄂伦春自治旗| 聊城市| 那坡县| 鄂州市| 潜江市| 友谊县| 正安县| 左云县| 利川市| 禄劝| 静海县|