-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileAction.class.php
More file actions
139 lines (117 loc) · 4.65 KB
/
fileAction.class.php
File metadata and controls
139 lines (117 loc) · 4.65 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
128
129
130
131
132
133
134
135
136
137
138
139
<?php
class fileAction extends Action
{
/* ***********************************************************************
QxUploadMgr - provides an API for uploading one or multiple files
with progress feedback (on modern browsers), does not block the user
interface during uploads, supports cancelling uploads.
http://qooxdoo.org
Copyright:
2011 Zenesis Limited, http://www.zenesis.com
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
This software is provided under the same licensing terms as Qooxdoo,
please see the LICENSE file in the Qooxdoo project's top-level directory
for details.
Parts of this code is based on the work by Andrew Valums ([email protected])
and is covered by the GNU GPL and GNU LGPL2 licenses; please see
http://valums.com/ajax-upload/.
Authors:
* John Spackman ([email protected])
************************************************************************/
/**
* Handles the upload
* @param $uploadDirectory {String} the path to upload to
* @param $replaceOldFile {Boolean} whether to replace existing files
*/
public static function handleUpload($uploadDirectory, $replaceOldFile = FALSE) {
if (!is_writable($uploadDirectory))
throw new Exception("Server error. Upload directory isn't writable.");
/*if ($_SERVER['CONTENT_TYPE'] == "application/octet-stream")
return QxUploadMgr::handleApplicationOctet($uploadDirectory, $replaceOldFile);
else*/
return fileAction::handleMultipartFormData($uploadDirectory, $replaceOldFile);
}
/**
* Determins the filename for the upload
* @param $uploadDirectory {String} the path to upload to
* @param $originalName {String} the filename as given by the browser
* @param $replaceOldFile {Boolean} whether to replace existing files
*/
public static function getFilename($uploadDirectory, $originalName, $replaceOldFile) {
$pathinfo = pathinfo($originalName);
$filename = $uploadDirectory . '/' . $pathinfo['filename'] .'.' . $pathinfo['extension'];
if (!$replaceOldFile){
$index = 1;
while (file_exists($filename)) {
$filename = $uploadDirectory . '/' . $pathinfo['filename'] . '-' . $index . '.' . $pathinfo['extension'];
$index++;
}
}
return $filename;
}
/**
* Handles the upload where content type is "application/octet-stream"
* @param $uploadDirectory {String} the path to upload to
* @param $replaceOldFile {Boolean} whether to replace existing files
*/
public static function handleApplicationOctet($uploadDirectory, $replaceOldFile) {
$filename = fileAction::getFilename($uploadDirectory, $_SERVER['HTTP_X_FILE_NAME'], $replaceOldFile);
//error_log("Receiving application/octet-stream into $filename");
$input = fopen("php://input", "r");
$target = fopen($filename, "w");
$realSize = stream_copy_to_stream($input, $target);
fclose($input);
fclose($target);
if (isset($_SERVER["CONTENT_LENGTH"])) {
$expectedSize = (int)$_SERVER["CONTENT_LENGTH"];
if ($realSize != $expectedSize)
return array('error' => 'File is the wrong size');
}
return array('success'=>true);
}
/**
* Handles the upload where content type is "multipart/form-data"
* @param $uploadDirectory {String} the path to upload to
* @param $replaceOldFile {Boolean} whether to replace existing files
*/
public static function handleMultipartFormdata($uploadDirectory, $replaceOldFile) {
//error_log("hello, count()=" . $_FILES.count());
$filenames=array();
foreach ($_FILES as $file) {
//error_log("$file=" . $file);
$filename = fileAction::getFilename($uploadDirectory, $file['name'], $replaceOldFile);
//if server is windows ,we must convert utf8 to gbk
$os = (DIRECTORY_SEPARATOR=='\\')?"windows":'linux';
if($os=="windows")
{
$filenameGBK=iconv("UTF-8", "GBK", $filename);
}else{
$filenameGBK=$filename;
}
//error_log("Receiving multipart/formdata into $filename");
if (!move_uploaded_file($file['tmp_name'], $filenameGBK)) {
//error_log("Failed to move uploaded file from ". $file['tmp_name']. " to $filename");
return array('error' => 'Failed to move uploaded file');
}
else{
$filenames[]=$filename;
}
}
//return array('success'=>true);
//$filename='c:/tmp/1.png';
//$filenames[]=$filename;
return $filenames;
}
public function upload()
{
$subDirectory = isset($_REQUEST["subDirectory"]) ? "/" . $_REQUEST["subDirectory"] : "";
Log::write('写入文件夹' . $subDirectory, Log::INFO);
$result = fileAction::handleUpload('uploads' . $subDirectory);
// to pass data through iframe you will need to encode all html tags
//echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
$this->success($result);
}
}
?>