在Unity3D(C#)中创建文件夹并将文本文件上载到服务器(php)
我所拥有的: 我有一个简单的模块,可以将字符串转换为文本文件并存储在我的服务器中 C#/Unity Code在Unity3D(C#)中创建文件夹并将文本文件上载到服务器(php),c#,php,file,unity3d,text,C#,Php,File,Unity3d,Text,我所拥有的: 我有一个简单的模块,可以将字符串转换为文本文件并存储在我的服务器中 C#/Unity Code private IEnumerator UploadUserData(string _fileName) { string _data = ("With text name " + System.DateTime.Now.ToString ()); string _postDataURL = "https://nameofserver.com/upload
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端
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);
}
- 如果文件夹“Folder1”不存在,php端应该创建它
然后将文本文件“filename.txt”上载到该文件夹
- 如果该目录中存在“Folder1”,那么我希望php
将文本文件“filename.txt”上载到现有文件夹的脚本
“文件夹1”
来自联合方
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);
}
我应该如何提及Unity webrequest中的文件夹名称
非常感谢您抽出时间。非常感谢 就像我在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']);
该文件夹名称可能是PHP中的文件内容调用的一部分。@Draco18s非常感谢您的评论。我对php完全陌生,因此你能帮我写一段代码吗?谢谢不幸的是,我自己对PHP知之甚少。