使用PHP从C#(Unity游戏引擎)将照片保存到服务器

使用PHP从C#(Unity游戏引擎)将照片保存到服务器,c#,php,unity3d,unitywebrequest,C#,Php,Unity3d,Unitywebrequest,我目前正在进行一个Unity游戏引擎项目,在该项目中,我使用PHP将一张照片从游戏中用户的本地计算机发送到web服务器。我的目标是将文件路径存储在数据库中,并将服务器上的实际图像存储在子目录中 我可以成功地从Unity发送照片,PHP脚本正在获取有关照片的信息(文件名、大小、tmp_名称)。但是,我无法在服务器上实际存储照片 C#统一代码 IEnumerator SendFile(byte[] imageData, string path) { WWWForm form

我目前正在进行一个Unity游戏引擎项目,在该项目中,我使用PHP将一张照片从游戏中用户的本地计算机发送到web服务器。我的目标是将文件路径存储在数据库中,并将服务器上的实际图像存储在子目录中

我可以成功地从Unity发送照片,PHP脚本正在获取有关照片的信息(文件名、大小、tmp_名称)。但是,我无法在服务器上实际存储照片

C#统一代码

IEnumerator SendFile(byte[] imageData, string path)
    {

        WWWForm form = new WWWForm();
        string filename = Path.GetFileName(path);
        string filepathname = Data_Manager.steamID + "-" + Data_Manager.steamName + "-" + filename;


        form.AddBinaryData("userImage", imageData, filepathname, "image/png");
        form.AddField("steamID", Data_Manager.steamID);
        form.AddField("steamName", Data_Manager.steamName);

        using (UnityWebRequest www = UnityWebRequest.Post("http://www.roastpartygame.com/database/storeImage.php", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                // Print response
                Debug.Log(www.downloadHandler.text);
            }
        }
    }
上面的代码按预期工作,我从PHP回传的任何内容都会在Unity游戏中使用上面代码中的“Debug.Log(www.downloadHandler.text);”进行记录

PHP

        $fileName = $_FILES['userImage']['name']; 
        $target = 'images/'.$fileName;

        //$base64decodedString = base64_decode(urldecode($_FILES['userImage']));
        //file_put_contents($target, file_get_contents($_FILES['userImage']['name']));

        if (copy($_FILES['userImage']['tmp_name']), $target) echo 'MOVED FILE!';
        if (move_uploaded_file($_FILES['userImage']['tmp_name'], $target)) echo 'MOVED FILE!';
我一直在玩复制和移动上传的文件。我也尝试过使用base64,但是我在Unity中的C#脚本中使用base64发送照片时遇到了问题。当从PHP页面中的表单提交照片时,这非常简单

有人对如何将映像存储到服务器有什么建议吗

附加代码(在C#Unity中获取照片数据):

public void DirectImageFolder()
    {
        FileBrowser.SetFilters(true, new FileBrowser.Filter("Images", ".jpg", ".png"));
        FileBrowser.SetDefaultFilter(".jpg");
        FileBrowser.SetExcludedExtensions(".lnk", ".tmp", ".zip", ".rar", ".exe");

        FileBrowser.AddQuickLink("Users", "C:\\Users", null);
        //FileBrowser.ShowLoadDialog((path) => {
            // Check if photo selected

        //},
        //() => { Debug.Log("Canceled"); },
        //false, null, "Roast Party: Select Image", "Select");

        StartCoroutine(ShowLoadDialogCoroutine());
    }

    IEnumerator ShowLoadDialogCoroutine()
    {
        // Show a load file dialog and wait for a response from user
        // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load"
        yield return FileBrowser.WaitForLoadDialog(false, null, "Roast Party: Load Image", "Load Image");

        // Dialog is closed
        // Print whether a file is chosen (FileBrowser.Success)
        // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false)
        Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);

        // upload image to server
        if (FileBrowser.Success)
        {
            // If a file was chosen, read its bytes via FileBrowserHelpers
            // Contrary to File.ReadAllBytes, this function works on Android 10+, as well
            byte[] bytes = FileBrowserHelpers.ReadBytesFromFile(FileBrowser.Result);

            //send the binary data to web server
            StartCoroutine(SendFile(bytes, FileBrowser.Result));
        }
    }
一个定制的unity插件被用来抓取照片数据

编辑: 我在PHP中添加了“ini_set('display_errors',1);”,现在我在Unity中得到了这个消息


警告:复制(images/76561198086846783 Fraccas au.jpg):无法打开流:第11行的/hermes/walnaweb05a/b2967/moo.devfraccascom/roastparty/database/storeImage.php中没有这样的文件或目录

没有正确设置目标路径的简单错误。我需要退出我所在的目录,进入images文件夹

if(isset($_FILES['userImage'])) { 
        $fileName = $_FILES['userImage']['name']; 
        $target = '../images/'.$fileName;

        if (copy($_FILES['userImage']['tmp_name'], $target)) echo 'MOVED FILE!';
}
“$target='../images/'.$fileName;”而不是“$target='images/'.$fileName;”

用于从网站上传照片的旧php文件与图片文件夹位于同一目录下,我没有意识到这个错误


感谢您帮助解决@derHugo的问题

您是否尝试过一次复制一个和移动一个?如果两者都成功了呢?我真的不明白为什么
base64
。。二进制图像数据不是
base64字符串,对吗…?是的,我分别尝试过。教程建议使用Convert.tobase64字符串(imageData)。我只是想说我也试过这样做。你能告诉我你从哪里得到
imageData
吗?@derHugo是的,这篇文章已经更新了。