0

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; 
?>
Jack
  • 1
  • 1
  • Just a side note but what is this for `UnityWebRequest fileToUpload = new UnityWebRequest();` ? – derHugo Oct 12 '20 at 14:07
  • @derHugo seems redundant now that I think about it - I used that as I received an error without it. New to this! – Jack Oct 12 '20 at 14:20
  • I'm no PHP expert but also this `$fileNames = array_filter($_FILES['files']['name']);` looks quite redundant somehow .. I would also check for any errors after `yield return fileToUpload.SendWebRequest();` .. maybe the file isn't even found on local side? Or any other error occures so that you are actually uploading 0 bytes? – derHugo Oct 12 '20 at 14:36
  • The files seem to be getting found, as each of the filenames are loaded in correctly and posted to the database. They just have no data in them. So confusing! – Jack Oct 12 '20 at 14:40
  • well the filenames you upload is whatever you pass into `file_n` .. doesn't mean that file exists ... if not then the `fileToUpload` request might simply have an error and the `downloadHandler.data` an empty array ... best you add a `if (fileToUpload.isHttpError || fileToUpload.isNetworkError) { Debug.LogError(req.error); yield break; }` – derHugo Oct 12 '20 at 14:42
  • Okay - I retried this. I edited the code to if (fileToUpload.isHttpError || fileToUpload.isNetworkError) { Debug.LogError(fileToUpload.error); yield break; }. It says "cannot connect to destination host". – Jack Oct 12 '20 at 15:15
  • ;) .. so tell us what exactly are you passing into `file_n` ? – derHugo Oct 12 '20 at 15:26
  • file_n is (should be) a path to a file. On Quest, i use Application.persistentDataPath + '/a folder/thefile.txt' – Jack Oct 12 '20 at 15:31

1 Answers1

0

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.

Jack
  • 1
  • 1