|
|
<?php require_once('../ajax_asm.php'); // the response function
function verbose( $ok=1,$info=""){ // failure to upload throws 400 error
if( $ok==0 ){ http_response_code(400); }; die(json_encode(["ok"=>$ok, "info"=>$info])); }; if( isset( $_GET['DID'] ) ) { $DID = intval( $_GET['DID'] ); } else { verbose( 0,"No Device ID Supplied!"); }; // invalid upload
if( empty( $_FILES ) || $_FILES['file']['error'] ){ verbose( 0, "Failed to move uploaded file.[" . $_FILES['file']['error'] . "]" ); }; // upload destination
//$filePath = __DIR__ . DIRECTORY_SEPARATOR . "../../vault/working_dir/uploads";
$filePath = $SYSPath . 'lib/controller/data/uploads'; if( !file_exists($filePath) ){ if (!mkdir($filePath, 0777, true)) { verbose(0, "Failed to create $filePath"); }; }; $rfn = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"]; $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"]; $filePath = $filePath . DIRECTORY_SEPARATOR . $fileName; // dealing with the chunks
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; $out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if( $out ) { $in = @fopen($_FILES['file']['tmp_name'], "rb"); if( $in ){ while( $buff = fread( $in, 4096 ) ){ fwrite($out, $buff); }; } else { verbose(0, "Failed to open input stream"); }; @fclose($in); @fclose($out); @unlink($_FILES['file']['tmp_name']); } else { verbose(0, "Failed to open output stream"); }; // check if file was uploaded
if (!$chunks || $chunk == $chunks - 1) { $MD5 = md5_file( "{$filePath}.part" ); $Dest = $SYSPath . 'lib/controller/data/uploads/'.$MD5; rename("{$filePath}.part", $Dest ); $QS = "INSERT INTO `lrf_uploads`( `sensor_id`, `ts`, `status`, `md5`, `file`, `issue_reset`) VALUES({$DID}, strftime('%s'), -1, '{$MD5}', '{$rfn}', 0 ) ON CONFLICT(`md5`) DO UPDATE SET `status`=-1, `ts`=strftime('%s'), `issue_reset`=0"; $DATABASE->Query( $QS ); //if( createNewTask( 3, $fileName, 'Handle Upload File' ) == 0 ){
// unlink( $filePath );
// verbose(0, "Failed to create new Task");
//};
}; verbose(1, "Upload OK");?>
|