UnityWebRequest not sending the post data and files and chunkedTransfer is already false

  c#, php, post, unity3d, upload

I am currently making a course creator (contents including XML and video files) using Unity 2018.2.13f1. One of the functions is to upload the courses to server for people to share. The upload function is done by this:

IEnumerator UploadRoutine() {
    string xmlPath = testDirectory + @"courses" + testProjectName + ".xml";
    WWWForm form = new WWWForm();
    form.AddField("project_name", testProjectName.Replace(' ', '_'));
    var xmlFile = UnityWebRequest.Get(xmlPath);
    yield return xmlFile.SendWebRequest();
    form.AddBinaryData("xml", xmlFile.downloadHandler.data, Path.GetFileName(xmlPath).Replace(' ', '_'));
    var filePaths = Directory.GetFiles(testDirectory + @"" + testProjectName);
    var files = new UnityWebRequest[filePaths.Length];
    for (int i = 0; i < files.Length; i++) {
        files[i] = UnityWebRequest.Get(filePaths[i]);
        yield return files[i].SendWebRequest();
        form.AddBinaryData("upload[]", files[i].downloadHandler.data, Path.GetFileName(filePaths[i]));
    }
    var webRequest = UnityWebRequest.Post(uploadLink, form);
    webRequest.SetRequestHeader("Content-Type", "multipart/form-data");
    webRequest.chunkedTransfer = false;
    webRequest.useHttpContinue = false;
    webRequest.certificateHandler = new BypassCertificate();
    webRequest.SendWebRequest();
    while (!webRequest.isDone) {
        yield return null;
        Debug.LogFormat("Progress: {0}", webRequest.uploadProgress);
    }
    if (!string.IsNullOrEmpty(webRequest.error)) {
        Debug.LogError(webRequest.error);
    } else {
        if (!string.IsNullOrEmpty(webRequest.downloadHandler.text))
            Debug.Log(webRequest.downloadHandler.text);
    }
}

And the php looks like this:

<?php
try {
    var_dump($_FILES);
    echo "n";

    $project_name = $_POST['project_name'];
    $path = "/var/www/lic_store/";
    $course_path = $path . "courses/";

    $xml_file = $course_path . basename($_FILES['xml']['name']);
    $uploadOK = true;
    $xml_file_type = strtolower(pathinfo($xml_file, PATHINFO_EXTENSION));
    if ($xml_file_type != 'xml') {
        echo basename($_FILES['xml']['name']) . ' is not an xml file! Upload failed!';
        $uploadOK = false;
    }

    $total = count($_FILES['upload']['name']);
    
    for ($i = 0; $i < $total; $i++)
        echo 'File ' . $i . ': ' . $_FILES['upload']['name'][$i] . "n";
    
    if ($uploadOK) {
        if (file_exists($xml_file))
            unlink($xml_file);

        if (move_uploaded_file($_FILES['xml']['tmp_name'], $xml_file)) {
            echo basename($_FILES['xml']['name']) . ' successfully uploaded!';
        } else {
            $uploadOK = false;
            echo 'Unable to upload ' . basename($_FILES['xml']['name']) . '!';
        }
        
        if ($uploadOK) {
            if (file_exists($path . $project_name . '/')) {
                $files = glob($path . $project_name . '/*');
                foreach ($files as $file) {
                    if (is_dir($file))
                        rmdir($file);
                    else
                        unlink($file);
                }               
            } else {
                mkdir($path . $project_name);
            }

            for ($i = 0; $i < $total; $i++) {
                if ($uploadOK) {
                    $tmp_file_path = $_FILES['upload']['tmp_name'][$i];
                    if ($tmp_file_path != '') {
                        $new_path = $path . $project_name . '/' . basename($_FILES['upload']['name'][$i]);
                        if (move_uploaded_file($tmp_file_path, $new_path)) {
                            echo basename($_FILES['upload']['name'][$i]) . ' successfully uploaded!';
                        } else {
                            $uploadOK = false;
                            echo 'Unable to upload ' . basename($_FILES['upload']['name'][$i]) . '!';
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }
} catch (Exception $e) {
    echo 'Exception: ' . $e->getMessage(); 
}
?>

I tried testing the functions, and it sent nothing. var_dump($_POST) and var_dump($_FILES) were tested. They all had blank array. I searched everywhere. I even added webRequest.chunkedTransfer = false; and webRequest.useHttpContinue = false; and it still didn’t work. php.ini also seemed fine. Have I done something wrong in the code or there was more?

Source: Ask PHP

LEAVE A COMMENT