-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFile.php
More file actions
127 lines (100 loc) · 2.79 KB
/
File.php
File metadata and controls
127 lines (100 loc) · 2.79 KB
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
122
123
124
125
126
127
<?php
class File extends Resource {
/** Returns the extension of the provided file. */
protected function extension($file) {
return strtolower(substr($file, strrpos($file, '.') + 1));
}
/** The path of the file. */
private function path() {
// Make sure the id is not equal to the placeholder.
if ($this->id != $this->place_holder()) {
// Get the path of this file.
$path = $this->folder() . '/' . $this->id;
// Make sure the file exists...
if (file_exists($path)) {
// Return the path.
return $path;
}
}
return '';
}
/**
* Loads a file.
* @return Response
*/
public function load() {
// Get the file path.
$file = $this->path();
// If the file doesn't exist, then get the placeholder.
if (!file_exists($file)) {
$file = $this->folder() . '/' . $this->place_holder();
}
// If the file exists, then stream it to the browser.
if (file_exists($file) && ($fp = fopen($file, 'rb'))) {
$response = new Response(200, array(), array(
'Content-Type: image/png',
"Content-Length: " . filesize($file)
));
fpassthru($fp);
fclose($fp);
}
else {
$response = new Response(404);
}
return $response;
}
/**
* Saves a file.
* @return Response
*/
public function save() {
// The allowed extensions.
$allowed_ext = $this->allowed_extensions();
// Get the post name of the file.
$post_name = $this->post_name();
// See if our image upload exists.
if (array_key_exists($post_name, $_FILES) && $_FILES[$post_name]['error'] == 0) {
// Make sure the file path is valid.
if ($file = $this->path()) {
// Get the upload.
$new_file = $_FILES[$post_name];
// Check to see if this image has the extensions allowed.
if (!in_array($this->extension($new_file['name']), $allowed_ext)) {
return new Response(406, 'Only ' . implode(',', $allowed_ext) . ' files are allowed!');
}
// For now just delete the old file.
if (file_exists($file)) {
unlink($file);
}
// Now move the image upload to the upload directory.
if (move_uploaded_file($new_file['tmp_name'], $file)) {
return new Response(200, array('id' => $this->id));
}
}
}
// Return a 406 error.
return new Response(406);
}
/**
* Deletes a file.
* @return type
*/
public function delete() {
if ($file = $this->path() && file_exists($file)) {
unlink($file);
}
}
protected function folder() {
return 'files';
}
protected function place_holder() {
return '';
}
protected function allowed_extensions() {
return array();
}
protected function post_name() {
return 'file';
}
}
?>