You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity截图Post上传Web服务器失败求助(XAMPP测试)

解决Unity截图通过POST上传至XAMPP服务器无响应的问题

看起来你的截图本地存储功能正常,但Unity的POST请求和PHP脚本的处理逻辑存在几个关键问题,导致上传没有反应。我们一步步来修复:

1. 修复Unity的表单数据构造

你当前的代码只传递了文件字节,却没有指定文件名和MIME类型,这会让PHP无法正确识别上传的文件对象。修改SendImage()方法的表单部分:

public IEnumerator SendImage() {
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    // 补充文件名和MIME类型,让PHP能正确解析文件信息
    formData.Add(new MultipartFormFileSection("file", bytes, "screenshot.png", "image/png"));
    UnityWebRequest www = UnityWebRequest.Post("http://localhost/postimage-test.php", formData);
    
    // 新增调试:打印PHP返回的内容,方便排查后端问题
    yield return www.SendWebRequest();
    
    if (www.isNetworkError || www.isHttpError) {
        Debug.Log($"请求错误: {www.error}");
    } else {
        Debug.Log("表单上传完成!");
        Debug.Log($"PHP返回内容: {www.downloadHandler.text}");
    }
}

2. 修复PHP脚本的上传判断逻辑

你的PHP用if ($_POST)来判断请求,但上传文件时,$_POST可能为空(没有额外表单字段),导致直接走到else分支输出"No Post!",根本没处理文件上传。同时要正确检查文件上传的状态:

<!--For testing purpose, to see if I can upload an image by a html form. -->
<html>
<body>
<form action="postimage-test.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
// 改为检查是否有合法的文件上传,而非依赖$_POST
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == UPLOAD_ERR_OK) {
    $target_dir = "upload/";
    // 用时间戳生成唯一文件名,避免重复文件覆盖
    $file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    $target_file = $target_dir . time() . "." . $file_ext;
    $uploadOk = 1;
    $imageFileType = strtolower($file_ext);
    
    // 验证是否为真实图片
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
    
    // 可选:限制仅允许图片格式
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename($target_file). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
            // 调试用:打印具体错误信息
            // print_r(error_get_last());
        }
    }
} else {
    // 区分无文件上传和上传出错的情况
    if(isset($_FILES["file"])){
        echo "上传错误: 错误码 " . $_FILES["file"]["error"];
    } else {
        echo "No file uploaded.";
    }
}
?>

3. 额外检查项

  • 确保XAMPP的upload文件夹有写入权限(Windows默认没问题,Linux/macOS可执行chmod 777 upload设置权限)
  • 若Unity是WebGL平台,需确认XAMPP配置了跨域允许;其他平台需开启网络权限(比如Android的INTERNET权限)
  • 查看XAMPP的Apache错误日志(xampp/apache/logs/error.log),PHP的运行错误会在这里记录

修改后,你可以通过Unity控制台查看PHP返回的内容,快速定位剩余问题。

内容的提问来源于stack exchange,提问作者Emu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:48:15