summaryrefslogtreecommitdiffstats
path: root/apis/webservice/upload.php
blob: 50ada40e71aa1472f401eaa2176e940712d1cee3 (plain) (blame)
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
<?php
if(!isset($_POST['uploadid'])){
    echo json_encode(array(
        "errormsg"=>"Not logged in",
        "status" => "error",
        "msg" => ""));
    die();
}elseif (!isset($_FILES['fileToUpload'])){
    echo json_encode(array(
        "errormsg"=>"No file received",
        "status" => "error",
        "msg" => ""));
    die();
}

$upload = Database::queryFirst("Select * from upload where token = :token",
             array( "token" => $_POST['uploadid']));
if($upload['userid']!= $_SESSION['userid']){
    echo json_encode(array(
        "errormsg"=>"Not same owner",
        "status" => "error",
        "msg" => ""));
    die();
}

$name = $_FILES["fileToUpload"]["name"];
$upload['nremaining'] = $upload['nremaining'] - 1;
if ($upload['nremaining'] < 0){
        echo json_encode(array(
            "errormsg"=>"Already received all the parts",
            "status" => "error",
            "msg" => ""));
        die();
}
$target_file = $target_dir.$_POST['uploadid']."/".$name;
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
    $ret = Database::exec("UPDATE upload SET nremaining= :nremaining".
        " WHERE id=:id", array(
            "id"=>$upload['id'],
            "nremaining"=>$upload['nremaining']
    ));
    if ($upload['nremaining'] == 0) {
        echo json_encode(array(
            "errormsg"=>"",
            "status" => "ok",
            "msg" => "Upload successful, sending to taskmanager"));
        //passa pro taskmanager;
        die();
    }else{
        echo json_encode(array(
            "errormsg"=>"",
            "status" => "ok",
            "msg" => "Upload successful, waiting next part"));
        die();
    }
} else {
    echo json_encode(array(
        "errormsg"=>"",
        "status" => "error",
        "msg" => "Error on upload, please resend"));
}