【问题标题】:Uploaded files are 0 bytes when uploading from Oculus Quest to LAMP server从 Oculus Quest 上传到 LAMP 服务器时,上传的文件为 0 字节
【发布时间】:2020-10-12 13:50:41
【问题描述】:

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

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

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

你们有什么想法吗?

统一代码: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; 
?>

【问题讨论】:

  • 只是一个旁注,但这对UnityWebRequest fileToUpload = new UnityWebRequest(); 来说是什么?
  • @derHugo 现在我想起来似乎是多余的——我使用了它,因为我收到了一个没有它的错误。新手!
  • 我不是 PHP 专家,但这个 $fileNames = array_filter($_FILES['files']['name']); 在某种程度上看起来很多余.. 我还会检查yield return fileToUpload.SendWebRequest(); 之后的任何错误.. 甚至可能在本地找不到该文件?或者发生任何其他错误,以便您实际上上传 0 个字节?
  • 似乎找到了文件,因为每个文件名都已正确加载并发布到数据库。他们只是没有数据。好混乱!
  • 那么您上传的文件名就是您传递给file_n 的任何文件名......并不意味着该文件存在......如果不存在,那么fileToUpload 请求可能只是有一个错误,而@987654328 @一个空数组...最好添加一个if (fileToUpload.isHttpError || fileToUpload.isNetworkError) { Debug.LogError(req.error); yield break; }

标签: php unity3d server upload quest


【解决方案1】:

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

fileToUpload = UnityWebRequest.Get(file_n);

到这里:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2015-02-27
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多