欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-06 08:57:13
本文实例讲述了php实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下: 文件操作类:

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

文件操作类:

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

更多关于php相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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