PHP to implement file operation class and file download function tutorial

Source: Internet
Author: User
Tags getmessage

This article describes the PHP implementation of the file operation class and file download function. Share to everyone for your reference, specific as follows:

File Action class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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, Lee Babin (lee@thecodeshoppe.com)
This code may is 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.
Constfounderror = "Sorry, the file in question does not exist.";
Constpermerror = "Sorry, you don't have the proper permissions on this file";
Constopenerror = "Sorry, the file in question could is not opened.";
Constcloseerror = "Sorry, the file could not to 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) {
Ensure the file exists.
try{
if (file_exists ($this->thepath)) {
Now, we need to the 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::P ermerror);
}
}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 () {
The 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) {
The 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) {
The 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!");
?>

File Download:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.