Uploaded files are 0 bytes when uploading from Oculus Quest to LAMP server

216 views Asked by At

I am running a LAMP server to collect some data. I have created an Oculus Quest app which collects some data, and then POSTs it to the LAMP server. During my testing of the POST method before deployment on Quest, I was using Unity and Windows. The method works fine, files are uploaded with no issues.

However when deploying this on the Quest, the file names are uploaded and moved to the target directory, but the files are all 0 bytes.

I have researched potential issues, and have modified my php.ini file accordingly. I have increased the input time, the max file size, the max file number etc. I have also checked permissions and the error.log files. There are no errors inside there.

Do you guys have any ideas?

Unity Code: file_n is an array. It reads in all the filenames from a specific folder in the Quest.

    IEnumerator UploadFile(string subID)
    {
        UnityWebRequest fileToUpload = new UnityWebRequest();
        WWWForm form = new WWWForm();

        fileToUpload = UnityWebRequest.Get(file_n);
        yield return fileToUpload.SendWebRequest();

        form.AddField("sub_ID", subID);
        form.AddBinaryData("files[]", fileToUpload.downloadHandler.data, Path.GetFileName(file_n));
        UnityWebRequest req = UnityWebRequest.Post(hostName, form);
        yield return req.SendWebRequest();

        if (req.isHttpError || req.isNetworkError)
        {
            Debug.Log(req.error);
        }
        else
        {
            Debug.Log(req.downloadHandler.text);
            result = req.downloadHandler.text;
            debugText.text = result;
        }
    }

PHP code:

<?php 

// Include the database configuration file 
include_once 'configFile.php'; 
     
// File upload configuration 
$targetDir = "targetDirectory/";  
$allowTypes = array('txt'); 
     
// variables submitted by user
$subID = $_POST["sub_ID"]; 

$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
$fileNames = array_filter($_FILES['files']['name']);
if(!empty($fileNames)){ 
    foreach($_FILES['files']['name'] as $key=>$val){ 
        // File upload path 
        $fileName = basename($_FILES['files']['name'][$key]); 
        $targetFilePath = $targetDir . $fileName;             
         
        // Check whether file type is valid 
        $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
        if(in_array($fileType, $allowTypes)){ 
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
                $insertValuesSQL .= "('" .$subID. "', '".$fileName."', NOW()),"; 
            }else{ 
                $errorUpload .= $_FILES['files']['name'][$key].' | '; 
            } 
        }else{ 
            $errorUploadType .= $_FILES['files']['name'][$key].' | '; 
        } 
    } 

    if(!empty($insertValuesSQL)){ 
        $insertValuesSQL = trim($insertValuesSQL, ','); 
         
        $insert = $conn->query("INSERT INTO thisTable (sub_ID, file_name, uploaded_on) VALUES $insertValuesSQL"); 
        if($insert){ 
            $errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
            $errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
            $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
            $statusMsg = "Files are uploaded successfully.".$errorMsg; 
        }else{ 
            $statusMsg = "Sorry, there was an error uploading your file."; 
        } 
    } 
}else{ 
    $statusMsg = 'Please select a file to upload.'; 
}      
// Display status message 
echo $statusMsg; 
?>
1

There are 1 answers

0
Jack On

Quest uses an Android system. I fixed it by changing this:

fileToUpload = UnityWebRequest.Get(file_n);

to this:

fileToUpload = UnityWebRequest.Get("file:///" + file_n);

This tells android that the uri passed into UnityWebRequest.Get() is a file destination and not a host. On windows it is not necessary to put this in, hence the error. Thanks to derHugo for the help.