-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.php
146 lines (122 loc) · 4.99 KB
/
fileupload.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Handle file uploads via XMLHttpRequest
*/
include ("include.common.php");
include_once ('server.includes.inc.php');
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
class qqUploadedFileForm {
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
function save($path) {
if(!move_uploaded_file($_FILES['file']['tmp_name'], $path)){
return false;
}
return true;
}
function getName() {
return $_FILES['file']['name'];
}
function getSize() {
return $_FILES['file']['size'];
}
}
class qqFileUploader {
var $log = null;
private $allowedExtensions = array();
private $sizeLimit = 10485760;
//private $sizeLimit = 2485760;
private $file;
function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;
$this->checkServerSettings();
$this->file = new qqUploadedFileForm();
}
private function checkServerSettings(){
$postSize = $this->toBytes(ini_get('post_max_size'));
$uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
/*if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
$size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
}*/
}
private function toBytes($str){
$val = trim($str);
$last = strtolower($str[strlen($str)-1]);
switch($last) {
case 'g': $val *= 1024;
case 'm': $val *= 1024;
case 'k': $val *= 1024;
}
return $val;
}
/**
* Returns array('success'=>1) or array('error'=>'error message')
*/
function handleUpload($uploadDirectory,$saveFileName, $replaceOldFile = FALSE){
if (!is_writable($uploadDirectory)){
return array('success'=>0,'error' => "Server error. Upload directory isn't writable.");
}
if (!$this->file){
return array('success'=>0,'error' => 'No files were uploaded.');
}
$size = $this->file->getSize();
error_log('file size ='.$size);
error_log('file size limit ='.$this->sizeLimit);
if ($size == 0) {
return array('success'=>0,'error' => 'File is empty');
}
if ($size > $this->sizeLimit) {
return array('success'=>0,'error' => 'File is too large');
}
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = $pathinfo['extension'];
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array('success'=>0,'error' => 'File has an invalid extension, it should be one of '. $these . '.');
}
//$filename .= microtime(true);
$filename = $saveFileName; // file with only name
$saveFileName = $saveFileName.'.'.strtolower($ext); // file with extention
$final_img_location = $uploadDirectory . $saveFileName;
if ($this->file->save($final_img_location)){
$arr = explode("/", $final_img_location);
return array('success'=>1,'filename'=>$arr[count($arr)-1],'error'=>'');
} else {
return array('success'=>0,'error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
}
}
//Generate File Name
$saveFileName = $_POST['file_name'];
if(empty($saveFileName) || $saveFileName == "_NEW_"){
$saveFileName = microtime();
$saveFileName = str_replace(".", "-", $saveFileName);
}
$file = new File();
$file->Load("name = ?",array($saveFileName));
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
$allowedExtensions = explode(',', FILE_TYPES);
// max file size in bytes
$sizeLimit =MAX_FILE_SIZE_KB * 1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload(CLIENT_BASE_PATH.'data/',$saveFileName);
// to pass data through iframe you will need to encode all html tags
if($result['success'] == 1){
$file->name = $saveFileName;
$file->filename = $result['filename'];
$file->employee = $_POST['user']=="_NONE_"?null:$_POST['user'];
$file->file_group = $_POST['file_group'];
$file->Save();
$result['filename'] = CLIENT_BASE_URL.'data/'.$result['filename'];
}
echo "<script>parent.closeUploadDialog(".$result['success'].",'".$result['error']."','".$result['filename']."');</script>";