| 1 |
<?php |
| 2 |
// summary |
| 3 |
// Test file to handle image uploads (remove the image size check to upload non-images) |
| 4 |
// |
| 5 |
// This file handles both Flash and HTML uploads |
| 6 |
// |
| 7 |
// NOTE: This is obviously a PHP file, and thus you need PHP running for this to work |
| 8 |
// NOTE: Directories must have write permissions |
| 9 |
// NOTE: This code uses the GD library (to get image sizes), that sometimes is not pre-installed in a |
| 10 |
// standard PHP build. |
| 11 |
// |
| 12 |
require("cLOG.php"); |
| 13 |
|
| 14 |
function findTempDirectory() |
| 15 |
{ |
| 16 |
if(isset($_ENV["TMP"]) && is_writable($_ENV["TMP"])) return $_ENV["TMP"]; |
| 17 |
elseif( is_writable(ini_get('upload_tmp_dir'))) return ini_get('upload_tmp_dir'); |
| 18 |
elseif(isset($_ENV["TEMP"]) && is_writable($_ENV["TEMP"])) return $_ENV["TEMP"]; |
| 19 |
elseif(is_writable("/tmp")) return "/tmp"; |
| 20 |
elseif(is_writable("/windows/temp")) return "/windows/temp"; |
| 21 |
elseif(is_writable("/winnt/temp")) return "/winnt/temp"; |
| 22 |
else return null; |
| 23 |
} |
| 24 |
function trace($txt, $isArray=false){ |
| 25 |
//creating a text file that we can log to |
| 26 |
// this is helpful on a remote server if you don't |
| 27 |
//have access to the log files |
| 28 |
// |
| 29 |
$log = new cLOG("../tests/upload.txt", false); |
| 30 |
//$log->clear(); |
| 31 |
if($isArray){ |
| 32 |
$log->printr($txt); |
| 33 |
}else{ |
| 34 |
$log->write($txt); |
| 35 |
} |
| 36 |
|
| 37 |
//echo "$txt<br>"; |
| 38 |
} |
| 39 |
function getImageType($filename){ |
| 40 |
return strtolower(substr(strrchr($filename,"."),1)); |
| 41 |
} |
| 42 |
trace("---------------------------------------------------------"); |
| 43 |
|
| 44 |
// |
| 45 |
// |
| 46 |
// EDIT ME: According to your local directory structure. |
| 47 |
// NOTE: Folders must have write permissions |
| 48 |
// |
| 49 |
$upload_path = "../tests/uploads/"; // where image will be uploaded, relative to this file |
| 50 |
$download_path = "../tests/uploads/"; // same folder as above, but relative to the HTML file |
| 51 |
|
| 52 |
// |
| 53 |
// NOTE: maintain this path for JSON services |
| 54 |
// |
| 55 |
require("../../../dojo/testsDOH/resources/JSON.php"); |
| 56 |
$json = new Services_JSON(); |
| 57 |
|
| 58 |
// |
| 59 |
// Determine if this is a Flash upload, or an HTML upload |
| 60 |
// |
| 61 |
// |
| 62 |
|
| 63 |
// First combine relavant postVars |
| 64 |
$postdata = array(); |
| 65 |
$htmldata = array(); |
| 66 |
$data = ""; |
| 67 |
trace("POSTDATA: " . count($_FILES) . " FILES"); |
| 68 |
foreach ($_POST as $nm => $val) { |
| 69 |
$data .= $nm ."=" . $val . ","; // string for flash |
| 70 |
$postdata[$nm] = $val; // array for html |
| 71 |
} |
| 72 |
|
| 73 |
trace($postdata, true); |
| 74 |
|
| 75 |
foreach ($_FILES as $nm => $val) { |
| 76 |
trace(" file: ".$nm ."=" . $val); |
| 77 |
} |
| 78 |
|
| 79 |
foreach ($_GET as $nm => $val) { |
| 80 |
trace($nm ."=" . $val); |
| 81 |
} |
| 82 |
|
| 83 |
$fieldName = "flashUploadFiles";//Filedata"; |
| 84 |
|
| 85 |
if( isset($_FILES[$fieldName]) || isset($_FILES['uploadedfileFlash'])){ |
| 86 |
// |
| 87 |
// If the data passed has $fieldName, then it's Flash. |
| 88 |
// NOTE: "Filedata" is the default fieldname, but we're using a custom fieldname. |
| 89 |
// The SWF passes one file at a time to the server, so the files come across looking |
| 90 |
// very much like a single HTML file. The SWF remembers the data and returns it to |
| 91 |
// Dojo as an array when all are complete. |
| 92 |
// |
| 93 |
trace("returnFlashdata...."); |
| 94 |
|
| 95 |
trace(""); |
| 96 |
trace("ID:"); |
| 97 |
|
| 98 |
trace("Flash POST:"); |
| 99 |
trace($_POST, true); |
| 100 |
|
| 101 |
|
| 102 |
$returnFlashdata = true; //for dev |
| 103 |
|
| 104 |
if( isset($_FILES[$fieldName])){ |
| 105 |
// backwards compat - FileUploader |
| 106 |
trace("FILES:"); |
| 107 |
trace($_FILES[$fieldName], true); |
| 108 |
$m = move_uploaded_file($_FILES[$fieldName]['tmp_name'], $upload_path . $_FILES[$fieldName]['name']); |
| 109 |
$name = $_FILES[$fieldName]['name']; |
| 110 |
|
| 111 |
}else{ |
| 112 |
// New fieldname - Uploader |
| 113 |
trace("FILES:"); |
| 114 |
trace($_FILES['uploadedfileFlash'], true); |
| 115 |
$m = move_uploaded_file($_FILES['uploadedfileFlash']['tmp_name'], $upload_path . $_FILES['uploadedfileFlash']['name']); |
| 116 |
$name = $_FILES['uploadedfileFlash']['name']; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
$file = $upload_path . $name; |
| 121 |
try{ |
| 122 |
list($width, $height) = getimagesize($file); |
| 123 |
} catch(Exception $e){ |
| 124 |
$width=0; |
| 125 |
$height=0; |
| 126 |
} |
| 127 |
$type = getImageType($file); |
| 128 |
trace("file: " . $file ." ".$type." ".$width); |
| 129 |
// Flash gets a string back: |
| 130 |
|
| 131 |
//exit; |
| 132 |
|
| 133 |
$data .='file='.$file.',name='.$name.',width='.$width.',height='.$height.',type='.$type; |
| 134 |
if($returnFlashdata){ |
| 135 |
trace("returnFlashdata:\n======================="); |
| 136 |
trace($data); |
| 137 |
trace("======================="); |
| 138 |
// echo sends data to Flash: |
| 139 |
echo($data); |
| 140 |
// return is just to stop the script: |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
}elseif( isset($_FILES['uploadedfile0']) ){ |
| 145 |
// |
| 146 |
// Multiple files have been passed from HTML |
| 147 |
// |
| 148 |
$cnt = 0; |
| 149 |
trace("IFrame multiple POST:"); |
| 150 |
trace($postdata, true); |
| 151 |
|
| 152 |
$_post = $htmldata; |
| 153 |
$htmldata = array(); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
while(isset($_FILES['uploadedfile'.$cnt])){ |
| 158 |
trace("IFrame multiple POST"); |
| 159 |
$moved = move_uploaded_file($_FILES['uploadedfile'.$cnt]['tmp_name'], $upload_path . $_FILES['uploadedfile'.$cnt]['name']); |
| 160 |
trace("moved:" . $moved ." ". $_FILES['uploadedfile'.$cnt]['name']); |
| 161 |
if($moved){ |
| 162 |
$name = $_FILES['uploadedfile'.$cnt]['name']; |
| 163 |
$file = $upload_path . $name; |
| 164 |
$type = getImageType($file); |
| 165 |
try{ |
| 166 |
list($width, $height) = getimagesize($file); |
| 167 |
} catch(Exception $e){ |
| 168 |
$width=0; |
| 169 |
$height=0; |
| 170 |
} |
| 171 |
trace("file: " . $file ); |
| 172 |
|
| 173 |
$_post['file'] = $download_path . $name; |
| 174 |
$_post['name'] = $name; |
| 175 |
$_post['width'] = $width; |
| 176 |
$_post['height'] = $height; |
| 177 |
$_post['type'] = $type; |
| 178 |
$_post['uploadType'] = $postdata['uploadType']; |
| 179 |
$_post['size'] = filesize($file); |
| 180 |
$_post['additionalParams'] = $postdata; |
| 181 |
trace($_post['additionalParams'], true); |
| 182 |
|
| 183 |
$htmldata[$cnt] = $_post; |
| 184 |
|
| 185 |
foreach($postdata as $key => $value){ |
| 186 |
//$htmldata[ $key ] = $value; |
| 187 |
} |
| 188 |
|
| 189 |
}elseif(strlen($_FILES['uploadedfile'.$cnt]['name'])){ |
| 190 |
$htmldata[$cnt] = array("ERROR" => "File could not be moved: ".$_FILES['uploadedfile'.$cnt]['name']); |
| 191 |
} |
| 192 |
$cnt++; |
| 193 |
} |
| 194 |
trace("HTML multiple POST done:"); |
| 195 |
foreach($htmldata as $key => $value){ |
| 196 |
trace($value, true); |
| 197 |
} |
| 198 |
|
| 199 |
}elseif( isset($_POST['uploadedfiles']) ){ |
| 200 |
trace("HTML5 multi file input... CAN'T ACCESS THIS OBJECT! (POST[uploadedfiles])"); |
| 201 |
trace(count($_POST['uploadedfiles'])." "); |
| 202 |
|
| 203 |
|
| 204 |
}elseif( isset($_FILES['uploadedfiles']) ){ |
| 205 |
// |
| 206 |
// If the data passed has 'uploadedfiles' (plural), then it's an HTML5 multi file input. |
| 207 |
// |
| 208 |
$cnt = 0; |
| 209 |
trace("HTML5 multi file input"); |
| 210 |
//trace($_FILES, true); |
| 211 |
//print_r($_FILES); |
| 212 |
$_post = $postdata; |
| 213 |
trace("POST DATA:::"); |
| 214 |
trace($_post, true); |
| 215 |
$htmldata = array(); |
| 216 |
$len = count($_FILES['uploadedfiles']['name']); |
| 217 |
// |
| 218 |
// Ugh. The array passed from HTML to PHP is fugly. |
| 219 |
// |
| 220 |
|
| 221 |
//print_r($_FILES['uploadedfiles']); |
| 222 |
|
| 223 |
for($i=0;$i<$len;$i++){ |
| 224 |
$moved = move_uploaded_file($_FILES['uploadedfiles']['tmp_name'][$i], $upload_path . $_FILES['uploadedfiles']['name'][$i]); |
| 225 |
trace("moved:" . $moved ." ". $_FILES['uploadedfiles']['name'][$i]); |
| 226 |
if($moved){ |
| 227 |
$name = $_FILES['uploadedfiles']['name'][$i]; |
| 228 |
$file = $upload_path . $name; |
| 229 |
$type = getImageType($file); |
| 230 |
try{ |
| 231 |
list($width, $height) = getimagesize($file); |
| 232 |
} catch(Exception $e){ |
| 233 |
error_log("NO EL MOVEO: " . $name); |
| 234 |
$width=0; |
| 235 |
$height=0; |
| 236 |
$_post['filesInError'] = $name; |
| 237 |
} |
| 238 |
|
| 239 |
if(!$width){ |
| 240 |
$_post['filesInError'] = $name; |
| 241 |
$width=0; |
| 242 |
$height=0; |
| 243 |
} |
| 244 |
trace("file: " . $file ." size: " . $width." ".$height); |
| 245 |
|
| 246 |
$_post['file'] = $download_path . $name; |
| 247 |
$_post['name'] = $name; |
| 248 |
$_post['width'] = $width; |
| 249 |
$_post['height'] = $height; |
| 250 |
$_post['type'] = $type; |
| 251 |
$_post['size'] = filesize($file); |
| 252 |
//$_post['additionalParams'] = $postdata; |
| 253 |
//trace($_post, true); |
| 254 |
|
| 255 |
$htmldata[$cnt] = $_post; |
| 256 |
|
| 257 |
}elseif(strlen($_FILES['uploadedfiles']['name'][$i])){ |
| 258 |
$htmldata[$cnt] = array("ERROR" => "File could not be moved: ".$_FILES['uploadedfiles']['name'][$i]); |
| 259 |
} |
| 260 |
$cnt++; |
| 261 |
} |
| 262 |
|
| 263 |
$data = $json->encode($htmldata); |
| 264 |
trace($data); |
| 265 |
print $data; |
| 266 |
return $data; |
| 267 |
|
| 268 |
}elseif( isset($_FILES['uploadedfile']) ){ |
| 269 |
// |
| 270 |
// If the data passed has 'uploadedfile', then it's HTML. |
| 271 |
// There may be better ways to check this, but this *is* just a test file. |
| 272 |
// |
| 273 |
$m = move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $upload_path . $_FILES['uploadedfile']['name']); |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
trace("HTML single POST:"); |
| 278 |
trace($postdata, true); |
| 279 |
|
| 280 |
$name = $_FILES['uploadedfile']['name']; |
| 281 |
$file = $upload_path . $name; |
| 282 |
$type = getImageType($file); |
| 283 |
try{ |
| 284 |
list($width, $height) = getimagesize($file); |
| 285 |
} catch(Exception $e){ |
| 286 |
$width=0; |
| 287 |
$height=0; |
| 288 |
} |
| 289 |
trace("file: " . $file ); |
| 290 |
|
| 291 |
$htmldata['file'] = $download_path . $name; |
| 292 |
$htmldata['name'] = $name; |
| 293 |
$htmldata['width'] = $width; |
| 294 |
$htmldata['height'] = $height; |
| 295 |
$htmldata['type'] = $type; |
| 296 |
$htmldata['uploadType'] = $uploadType; |
| 297 |
$htmldata['size'] = filesize($file); |
| 298 |
$htmldata['additionalParams'] = $postdata; |
| 299 |
|
| 300 |
$data = $json->encode($htmldata); |
| 301 |
trace($data); |
| 302 |
print $data; |
| 303 |
return $data; |
| 304 |
|
| 305 |
|
| 306 |
}elseif(isset($_GET['rmFiles'])){ |
| 307 |
trace("DELETING FILES" . $_GET['rmFiles']); |
| 308 |
$rmFiles = explode(";", $_GET['rmFiles']); |
| 309 |
foreach($rmFiles as $f){ |
| 310 |
if($f && file_exists($f)){ |
| 311 |
trace("deleted:" . $f. ":" .unlink($f)); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
}else{ |
| 316 |
trace("IMROPER DATA SENT... $_FILES:"); |
| 317 |
trace($_FILES); |
| 318 |
$htmldata = array("ERROR" => "Improper data sent - no files found"); |
| 319 |
} |
| 320 |
|
| 321 |
//HTML gets a json array back: |
| 322 |
$data = $json->encode($htmldata); |
| 323 |
trace("Json Data Returned:"); |
| 324 |
trace($data); |
| 325 |
// in a text field: |
| 326 |
?> |
| 327 |
|
| 328 |
<textarea style="width:600px; height:150px;"><?php print $data; ?></textarea> |