This repository was archived by the owner on Aug 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathXHR.php
More file actions
78 lines (69 loc) · 1.83 KB
/
XHR.php
File metadata and controls
78 lines (69 loc) · 1.83 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
<?php
/**
* Uploader_XHRSubmission
* @note an XHR upload handler
*/
class Uploader_XHRSubmission {
private $fileKey;
private $tmp_location;
private $tmp_handle;
private $field;
public function __construct($fileKey, $field) {
$this->fileKey = $fileKey;
$this->field = $field;
}
/**
* Save the input stream to a path on disk
* @return boolean TRUE on success
*/
public function saveToTmp() {
$input = fopen("php://input", "r");
$this->tmp_location = tempnam(sys_get_temp_dir(), 'xhr_upload_');
$this->tmp_handle = fopen($this->tmp_location, "w");
stream_copy_to_stream($input, $this->tmp_handle);
fclose($input);
fclose($this->tmp_handle);
return true;
}
//save file, at this point all checks have been done
public function save($path) {
if(file_exists($this->tmp_location)) {
$result = rename($this->tmp_location, $path);
if(!$result) {
throw new Exception('Could not save uploaded file. Can the destination path be written to?');
}
@chmod($path, $this->field->GetFilePermission());
} else {
throw new Exception('Could not save uploaded file. The uploaded file no longer exists.');
}
return TRUE;
}
public function cleanup() {
if(is_resource($this->tmp_handle)) {
fclose($this->tmp_handle);
}
if(file_exists($this->tmp_location)) {
unlink($this->tmp_location);
}
}
public function getTmpFile() {
return $this->tmp_location;
}
public function getName() {
return $_GET[$this->fileKey];
}
public function getSize() {
if(file_exists($this->tmp_location)) {
return filesize($this->tmp_location);
}
return 0;
}
public function getMimeType() {
$mimeType = DisplayAnythingFile::MimeType($this->tmp_location);
if(!$mimeType['mimetype']) {
throw new Exception("Cannot reliably determine the mime-type of this file");
}
return $mimeType['mimetype'];
}
}
?>