4 * Handle file uploads via XMLHttpRequest
6 class qqUploadedFileXhr {
8 * Save the file to the specified path
9 * @return boolean TRUE on success
11 function save($path) {
12 $input = fopen("php://input", "r");
14 $realSize = stream_copy_to_stream($input, $temp);
17 if ($realSize != $this->getSize()){
21 $target = fopen($path, "w");
22 fseek($temp, 0, SEEK_SET);
23 stream_copy_to_stream($temp, $target);
29 return $_GET['qqfile'];
32 if (isset($_SERVER["CONTENT_LENGTH"])){
33 return (int)$_SERVER["CONTENT_LENGTH"];
35 throw new Exception('Getting content length is not supported.');
41 * Handle file uploads via regular form post (uses the $_FILES array)
43 class qqUploadedFileForm {
45 * Save the file to the specified path
46 * @return boolean TRUE on success
48 function save($path) {
49 if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
55 return $_FILES['qqfile']['name'];
58 return $_FILES['qqfile']['size'];
62 class qqFileUploader {
63 private $allowedExtensions = array();
64 private $sizeLimit = 10485760;
67 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
68 $allowedExtensions = array_map("strtolower", $allowedExtensions);
70 $this->allowedExtensions = $allowedExtensions;
71 $this->sizeLimit = $sizeLimit;
73 $this->checkServerSettings();
75 if (isset($_GET['qqfile'])) {
76 $this->file = new qqUploadedFileXhr();
77 } elseif (isset($_FILES['qqfile'])) {
78 $this->file = new qqUploadedFileForm();
84 private function checkServerSettings(){
85 $postSize = $this->toBytes(ini_get('post_max_size'));
86 $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
88 if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
89 $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
90 die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
94 private function toBytes($str){
96 $last = strtolower($str[strlen($str)-1]);
98 case 'g': $val *= 1024;
99 case 'm': $val *= 1024;
100 case 'k': $val *= 1024;
106 * Returns array('success'=>true) or array('error'=>'error message')
108 function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
109 if (!is_writable($uploadDirectory)){
110 return array('error' => "Server error. Upload directory isn't writable.");
114 return array('error' => 'No files were uploaded.');
117 $size = $this->file->getSize();
120 return array('error' => 'File is empty');
123 if ($size > $this->sizeLimit) {
124 return array('error' => 'File is too large');
127 $pathinfo = pathinfo($this->file->getName());
128 $filename = $pathinfo['filename'];
129 //$filename = md5(uniqid());
130 $ext = $pathinfo['extension'];
132 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
133 $these = implode(', ', $this->allowedExtensions);
134 return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
137 if(!$replaceOldFile){
138 /// don't overwrite previous files that were uploaded
139 while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
140 $filename .= rand(10, 99);
144 if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
145 return array('success'=>true);
147 return array('error'=> 'Could not save uploaded file.' .
148 'The upload was cancelled, or server error encountered');
154 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
155 $allowedExtensions = array();
156 // max file size in bytes
157 $sizeLimit = 10 * 1024 * 1024;
159 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
160 $result = $uploader->handleUpload('uploads/');
161 // to pass data through iframe you will need to encode all html tags
162 echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);