PHP實現檔案操作類及檔案下載功能的教程

來源:互聯網
上載者:User

本文執行個體講述了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);
?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.