我想在我的应用程序中将图像上传到服务器,这是统一完成的,我在Unity论坛中使用了这个代码。当它运行时,它表示已成功上传但无法找到上传的文件。
我得到了
upload done :Success! tmpName: C:\xampp\tmp\php2EB.tmp size: 156070 mime: text/plain name: 66.jpg <br />
<b>Warning</b>: move_uploaded_file(../images/66.jpg): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\DreamHousedb\upload.php</b> on line <b>10</b><br />
<br />
但是这个文件确实存在于localhost
中如何解决此问题?
FileUpload.cs
using UnityEngine;
using System.Collections;
public class FileUpload : MonoBehaviour
{
private string m_LocalFileName = "C:/boot.ini";
private string m_URL = "http://localhost/DreamHousedb/upload.php";
IEnumerator UploadFileCo(string localFileName, string uploadURL)
{
WWW localFile = new WWW("file:///" + localFileName);
yield return localFile;
if (localFile.error == null)
Debug.Log("Loaded file successfully");
else
{
Debug.Log("Open file error: " + localFile.error);
yield break; // stop the coroutine here
}
WWWForm postForm = new WWWForm();
// version 1
//postForm.AddBinaryData("theFile",localFile.bytes);
// version 2
postForm.AddBinaryData("theFile", localFile.bytes, localFileName, "text/plain");
WWW upload = new WWW(uploadURL, postForm);
yield return upload;
if (upload.error == null)
Debug.Log("upload done :" + upload.text);
else
Debug.Log("Error during upload: " + upload.error);
}
void UploadFile(string localFileName, string uploadURL)
{
StartCoroutine(UploadFileCo(localFileName, uploadURL));
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
m_LocalFileName = GUILayout.TextField(m_LocalFileName);
m_URL = GUILayout.TextField(m_URL);
if (GUILayout.Button("Upload"))
{
UploadFile(m_LocalFileName, m_URL);
}
GUILayout.EndArea();
}
}
PHP脚本
<?php
if(isset($_FILES['theFile']))
{
print("Success! ");
print("tmpName: " . $_FILES['theFile']['tmp_name'] . " ");
print("size: " . $_FILES['theFile']['size'] . " ");
print("mime: " . $_FILES['theFile']['type'] . " ");
print("name: " . $_FILES['theFile']['name'] . " ");
move_uploaded_file($_FILES['theFile']['tmp_name'], "../images/" . $_FILES['theFile']['name']);
} else
{
print("Failed!");
}
?>