【问题标题】:Create folders and upload text files to server (php) in Unity3D (C#)在 Unity3D (C#) 中创建文件夹并将文本文件上传到服务器 (php)
【发布时间】:2018-02-12 21:07:01
【问题描述】:

我有什么:

我有一个简单的模块,可以将字符串转换为文本文件并将其存储在我的服务器中

C#/Unity 代码

private IEnumerator UploadUserData(string _fileName)
{        
    string _data = ("With text name " + System.DateTime.Now.ToString ());
    string _postDataURL = "https://nameofserver.com/upload.php"

    WWWForm _form = new WWWForm ();
    _form.AddField ("name", _fileName);
    _form.AddField ("data", _data);      

    UnityWebRequest _wwwRequest = UnityWebRequest.Post (_postDataURL, _form);

    yield return _wwwRequest.Send ();

    while (!_wwwRequest.isDone)
    {   yield return null;}

    if (_wwwRequest.error != null)
    {
        Debug.Log (_wwwRequest.error);
    }
    else
    {
        Debug.Log ("Uploaded");
    }

    Debug.Log (_wwwRequest.downloadHandler.text);
}

服务器端 PHP

 <?php    
if(isset($_POST['name']) && isset($_POST['data'])){
    file_put_contents($_POST['name'].".txt", $_POST['data']);      
    echo "uploaded.";
}else{
    echo "invalid file uploaded.";
}  

?>

请求

我想构建一个可以将文件上传到特定文件夹的系统。假设我要上传一个文本文件 (filename.txt) 到文件夹名称“Folder1”。

从php端

  • 如果不存在,php 端应该创建一个文件夹“Folder1” 然后将文本文件“filename.txt”上传到该文件夹​​

  • 如果该目录中存在“Folder1”,那么我想要 php 将该文本文件“filename.txt”上传到现有文件夹的脚本 “文件夹 1”

来自 Unity 方面

我应该如何在 Unity webrequest 中提及文件夹名称?

非常感谢您的宝贵时间。非常感谢。

【问题讨论】:

  • 该文件夹名称可能是 PHP 中 file_put_contents 调用的一部分。
  • @Draco18s 感谢您的评论。我对 php 完全陌生,因此你能帮我写一个代码 sn-p 吗?谢谢!
  • 很遗憾我自己对 PHP 知之甚少。

标签: c# php file unity3d text


【解决方案1】:

就像我在 IRC 上对你咆哮一样,你应该避免让你的文件路径由用户提供的数据,甚至是用户可访问的 API 指定,甚至包括在内。

我会建议一些类似的东西:

// always have something define the absolute path to your application root,
// then build your paths/filenames relative to that.
// let's say this is /usr/local/myapp/config/config.php
define('APPROOT', realpath(__DIR__ . '/..')); // APPROOT == '/usr/local/myapp'
define('USERUPLOADS', APPROOT . '/user_uploads');

// userid SHOULD be something you control, not a username or anything specified
// by the user. the focus is to prevent malformed and/or malevolent user data
// from breaking out of the upload directory sandbox.
function acceptUploadedUserData($userid, $name, $data) {
    $userdir = USERUPLOADS . '/' . $userid;
    if( ! is_dir($userdir) ) {
        mkdir($userdir);
    }
    // just kinda baseline "OK"
    if( strpos('..', $name) !== false || strpos('/', $name) !== false ) {
        throw new Exception('Specified name cannot contain .. or /');
    }
    file_put_contents($userdir . '/' . $name, $data);

    // better yet don't let the user have *any* control over any part of the path
    // but also allows you to specify *any* string as the filename.
    file_put_contents($userdir . '/' . md5($name), $data);
}

acceptUploadedUserData($_SESSION['user_id'], $_POST['name'], $_POST['data']);

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多