PHP实现文件操作类及文件下载功能的教程

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

文件操作类:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

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

 classcfile {

  //The path to the file we wish to work with.

  protected$thepath;

  //Error messages in the form of constants for ease of use.

  constFOUNDERROR ="Sorry, the file in question does not exist.";

  constPERMERROR ="Sorry, you do not have the proper permissions on this file";

  constOPENERROR ="Sorry, the file in question could not be opened.";

  constCLOSEERROR ="Sorry, the file could not be closed.";

  //The constructor function.

  publicfunction__construct (){

   $num_args= func_num_args();

   if($num_args> 0){

    $args= func_get_args();

    $this->thepath =$args[0];

   }

  }

  //A function to open the file.

  privatefunctionopenfile ($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{

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

    }

  }

  //A function to close a file.

  functionclosefile () {

    try{

      if(!fclose ($this->thepath)){

        thrownewexception (self::CLOSEERROR);

      }

    }catch(exception$e) {

      echo$e->getmessage();

    }

  }

  //A function to read a file, then return the results of the read in a string.

  publicfunctionread () {

    //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.

      returnfgets($filepointer);

    }

    //Lastly, close the file.

    $this->closefile ();

  }

  //A function to write to a file.

  publicfunctionwrite ($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.

      returnfwrite ($filepointer,$towrite);

    }

    //Lastly, close the file.

    $this->closefile ();

  }

  //A function to append to a file.

  publicfunctionappend ($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.

      returnfwrite ($filepointer,$toappend);

    }

    //Lastly, close the file.

    $this->closefile ();

  }

  //A function to set the path to a new file.

  publicfunctionsetpath ($newpath) {

    $this->thepath =$newpath;

  }

 }

?>


1

2

3

4

5

6

7

8

9

<?php

  $myfile=newcfile ("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!");

?>

文件下载:


1

2

3

4

5

6

7

8

9

10

11

12

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

?>

时间: 2024-10-28 13:37:32

PHP实现文件操作类及文件下载功能的教程的相关文章

Asp.net(c#)常用文件操作类封装 移动 复制 删除 上传 下载等

Asp.net(c#)中常用文件操作类封装 包括:移动 复制 删除 上传 下载等 using System; using System.Configuration; using System.Data; using System.IO; using System.Text; using System.Threading; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.Ht

用java写的一个文件操作类包

前几天仔细看了看java的I/O操作,呵呵.就写了一个操作文件的类包,功能有创建文件或目录,删除文件或目录,复制文件或目录,移动文件或目录,设置文件或目录属性,查看文件或目录大小.呵呵,功能比较简单,源代码为: 创建: Java代码 package fileOperation; import java.io.File; import java.io.FileOutputStream; /** * @author wakin * */ public class Create { /**根据字符串生

PHP面向对象文件操作类

对象 <?php  /*  文件名:ClassFile.php  功能:面向对象,文件操作类(查看文件,删除文件,上传文件)  作者:感染源  时间:2007-1-11 */  class ClassFile {  private $dir_name;  private $file_name;  private $dh;  //private $file_path;      //构造函数,打开工作文件目录  function __construct($PDirName)  {   $this-

Web项目: Java在部署项目的WebRoot下建立文件夹(附上文件操作类)

public boolean doTest(){ String path="../webapps/FileTest/reportFiles/aa.jsp";//FileTest为自己的项目名 reportFiles为自己建立的文件夹 aa.jsp为自己建立的文件 boolean isDone = false; File file = new File(path); if(file.exists()) throw new RuntimeException("File: &quo

php文件操作类(建立,写入,删除,修改,复制,移动,创建目录)

实例1  代码如下 复制代码 <?php    /*   文件名:ClassFile.php   功能:面向对象,文件操作类(查看文件,删除文件,上传文件)   作者:感染源   时间:2007-1-11  */    class ClassFile  {   private $dir_name;   private $file_name;   private $dh;   //private $file_path;       //构造函数,打开工作文件目录   function __cons

Python文件操作类操作实例代码

  #!/usr/bin/env python 01 #!/usr/bin/env python  02 #coding:utf-8  03 # Author: 酷酷 04 # Purpose: 文件操作类  05 # Created: 2011/1/1  06  07 #声明一个字符串文本  08 poem='''  09 Programming is fun测试  10 When the work is done  11 if you wanna make your work also fu

Python文件操作类操作实例详解_python

本文讲述了Python文件操作类的操作实例,详细代码如下: #!/usr/bin/env python #!/usr/bin/env python #coding:utf-8 # Purpose: 文件操作类 #声明一个字符串文本 poem=''' Programming is fun测试 When the work is done if you wanna make your work also fun: use Python! ''' #创建一个file类的实例,模式可以为:只读模式('r'

Python实现的ini文件操作类分享_python

类代码: # -*- coding:gbk -*- import ConfigParser, os class INIFILE: def __init__(self, filename): self.filename = filename self.initflag = False self.cfg = None self.readhandle = None self.writehandle = None def Init(self): self.cfg = ConfigParser.Confi

Python实现的tab文件操作类分享_python

类代码: # -*- coding:gbk -*- import os class TABFILE: def __init__(self, filename, dest_file = None): self.filename = filename if not dest_file: self.dest_file = filename else: self.dest_file = dest_file self.filehandle = None self.content = [] self.ini