首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C# (统一游戏引擎)用PHP将照片保存到服务器

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

Stack Overflow用户
提问于 2019-11-20 05:55:26
回答 1查看 1K关注 0票数 0

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

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

C#统一码

代码语言:javascript
复制
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中得到的任何内容都会使用上面代码中的"Debug.Log(www.downloadHandler.text);“记录在团结游戏中。

代码语言:javascript
复制
        $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!';

我玩过复制和move_uploaded_file。我也尝试过使用base64,但是我在使用base64从C#脚本中发送照片时遇到了问题。这在PHP页面中从表单提交照片时非常简单。

有人对如何将图像存储到服务器有任何建议吗?

附加代码(用C#统一抓取照片数据):

代码语言:javascript
复制
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));
        }
    }

一个定制的统一插件正在被用来抓取照片数据。

编辑:我在PHP中添加了“ini_set(‘display_errors’,1)”,我现在在Unity中得到了这条消息。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-20 07:03:57

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

代码语言:javascript
复制
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 =‘$target/’.$fileName;”

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58947619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档