首页 > 解决方案 > 从 Oculus Quest 上传到 LAMP 服务器时,上传的文件为 0 字节

问题描述

我正在运行 LAMP 服务器来收集一些数据。我创建了一个 Oculus Quest 应用程序,它收集一些数据,然后将其发布到 LAMP 服务器。在部署到 Quest 之前测试 POST 方法期间,我使用的是 Unity 和 Windows。该方法工作正常,文件上传没有问题。

但是在 Quest 上部署时,文件名被上传并移动到目标目录,但文件都是 0 字节。

我研究了潜在的问题,并相应地修改了我的 php.ini 文件。我增加了输入时间、最大文件大小、最大文件数等。我还检查了权限和 error.log 文件。里面没有错误。

你们有什么想法吗?

Unity 代码:file_n 是一个数组。它从 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代码:

<?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; 
?>

标签: phpunity3dserveruploadquest

解决方案


Quest 使用 Android 系统。我通过更改它来修复它:

fileToUpload = UnityWebRequest.Get(file_n);

对此:

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

这告诉 android 传入 UnityWebRequest.Get() 的 uri 是文件目标而不是主机。在 Windows 上,没有必要将其放入,因此会出现错误。感谢 derHugo 的帮助。


推荐阅读