function handleUpload() { /*** * Determine the type of file upload handler that needs to be * used, then pass things along accordingly. ***/ if (empty($_FILES)) { return array('status' => false,'error' => 'No files provided','human_error' => 'Please provide a file to upload'); } $temp = $_FILES['file']['tmp_name']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $temp); finfo_close($finfo); $file = $_FILES['file']['name']; $mime_error = ""; if(empty($mime)) { # Just the fallback that is based purely on extension # Only used when finfo can't find a mime type try { $mime = mime_type($file); } catch (Exception $e) { $mime_error = $e->getMessage(); $mime = null; } } # Look at the MIME prefix $mime_types = explode('/', $mime); $mime_class = $mime_types[0]; # Now, call the actual uploader function based on the mime class # (eg, image/, audio/, video/ ... ) switch ($mime_class) { case 'image': return doUploadImage($mime); break; case 'audio': return doUploadAudio($mime); break; case 'video': return doUploadVideo($mime); break; default: # return array('status' => false,'error' => "Unrecognized MIME type '".$mime."' for file '".$file."' (".$mime_error.")", 'human_error' => 'Unsupported file format', "dumb_type"=>mime_type($file)); $temp = $_FILES['file']['tmp_name']; $uploadPath = $_REQUEST['uploadpath']; $savePath = dirname(__FILE__).'/'.$uploadPath; if (!file_exists($savePath)) { if(!mkdir($savePath)) { return array( 'status' => false, 'error' => "Bad path '$savePath'", 'human_error' => 'There is a server misconfiguration preventing your file from being uploaded', ); } } $file = $_FILES['file']['name']; $exploded = explode('.', $file); $extension = array_pop($exploded); $fileName = md5($file.microtime_float()); $newFilePath = $fileName.'.'.$extension; $fileWritePath = $savePath.$newFilePath; # We want to suppress the warning on move_uploaded_file, or else # it'll return an invalid JSON response #error_reporting(0); # Disable this for debugging $status = move_uploaded_file($temp, $fileWritePath); $uploadStatus = array('status' => $status,'original_file' => $file,'wrote_file' => $newFilePath,'full_path' => getRelativePath($fileWritePath), "mime_provided"=>$mime); return $uploadStatus; } }
$extension = array_pop(explode('.', $file)); $newFilePath = md5($file).'.'.$extension; $fileWritePath = $savePath.$newFilePath; return array('status' => move_uploaded_file($temp, $fileWritePath), 'original_file' => $file, 'wrote_file' => $newFilePath, 'full_path' => $fileWritePath); } if (isset($_SERVER['QUERY_STRING'])) { parse_str($_SERVER['QUERY_STRING'], $_REQUEST); } $do = isset($_REQUEST['do']) ? strtolower($_REQUEST['do']) : null; switch ($do) { case 'get_last_mod': returnAjax(getUserFileModTime($_REQUEST)); break; case 'upload_image': returnAjax(doUploadImage()); break; default: $default_answer = array( 'status' => false, 'error' => 'Invalid action', 'human_error' => 'No valid action was supplied.', 'provided_args' => $_REQUEST, 'requested_action' => $do, ); # doUploadImage() returnAjax($default_answer); }