文件上传PHP

<?php
	$targetIp = GetIP();
	$fileUpload = 'fileUpload';
	$frameCount = 'frameCount';
	$fileName = $_FILES[$fileUpload]["name"];
	$datestr = date("ymdhis");
	$spanPos = strripos($fileName,".");
	$sy = substr($fileName,$spanPos);
	$name = substr($fileName,0,$spanPos);
	$uploaddir = "D:/xampp/htdocs/upload/files/";
	$uploadfile = $uploaddir . $name."_".$targetIp.$datestr.$sy;
	
	if ($_FILES[$fileUpload]["error"] > 0){
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }else{
		echo "frameCount: ".$_POST[$frameCount]."\n";
		echo "Upload: " . $fileName."\n";
		echo "Type: " . $_FILES[$fileUpload]["type"]."\n";
		echo "Size: " . ($_FILES[$fileUpload]["size"] / 1024)."\n";
		echo "Temp file: " . $_FILES[$fileUpload]["tmp_name"]."\n";
		if(is_uploaded_file($_FILES[$fileUpload]["tmp_name"])){
			if (file_exists($uploadfile)){
				
				if(unlink($uploadfile)){
					echo('delete '.$uploadfile.' successful');
				}else{
					echo('delete '.$uploadfile.' fail');
				}
			}
			if(move_uploaded_file($_FILES[$fileUpload]["tmp_name"],$uploadfile)){
				echo 0;
			}else{
				echo "Stored in: " . $uploadfile."\n";
			}
		}else{
			echo 'no uploaded file';
		}
	}
	function GetIP(){
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){
			$cip = $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif(!empty($_SERVER["REMOTE_ADDR"])){
			$cip = $_SERVER["REMOTE_ADDR"];
		}
		else{
			$cip = "无法获取!";
		}
		return $cip;
	}
?>

  

using UnityEngine;
using System.Collections;
using System.IO;
public class TestUploadFiles : MonoBehaviour {

	private string screenShotURL = "http://localhost/upload/uploadlog.php";

    // Use this for initialization
    void Start()
    {
    }
	void OnGUI(){
		if(GUILayout.Button("UploadPNG")){
			StartCoroutine(UploadPNG());
		}
		if(GUILayout.Button("UploadText")){
			StartCoroutine(UploadText());
		}
		if(GUILayout.Button("UploadFiles")){
			StartCoroutine(UploadFiles());
		}
	}
    IEnumerator UploadPNG()
    {

		yield return new WaitForEndOfFrame();
		int width = Screen.width;
		int height = Screen.height;
		var tex = new Texture2D(width, height, TextureFormat.RGB24, false);

		tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
		tex.Apply();
		byte[] bytes = tex.EncodeToPNG();
		Destroy(tex);
		WWWForm form = new WWWForm();
		form.AddField("frameCount", Time.frameCount.ToString());
		form.AddBinaryData("fileUpload", bytes, "screenShot.txt");
		WWW w = new WWW(screenShotURL, form);
		yield return w;
		if (!string.IsNullOrEmpty(w.error))
		{
			print(w.error);
		}
		else
		{
			print (w.text);
			print("Finished Uploading Screenshot");
		}
    }
	IEnumerator UploadFiles(){
		string dirPath = "E:\\HBWork\\HBClient\\www.hobbits.dfplay.abtest\\log\\";
		string[] filePaths = Directory.GetFiles(dirPath);
		foreach(string file in filePaths){
			yield return new WaitForEndOfFrame();
			FileInfo fileInfo = new FileInfo (file);
			FileStream fileStream = File.OpenRead (file);
			byte[] bytes = new byte[fileStream.Length];
			fileStream.Read (bytes,0,bytes.Length);

			WWWForm form = new WWWForm();
			form.AddField("frameCount", Time.frameCount.ToString());
			form.AddBinaryData("fileUpload", bytes, fileInfo.FullName);
			WWW w = new WWW(screenShotURL, form);
			yield return w;
			if (!string.IsNullOrEmpty(w.error))
			{
				print(w.error);
			}
			else
			{
				if (w.text.ToString ().Equals ("0")) {
					print ("Successful");
				}
			}
		}
	}
	IEnumerator UploadText()
	{
		string logPath = "E:\\HBWork\\HBClient\\www.hobbits.dfplay.abtest\\log\\log_0.bak1.txt";
		FileStream fileStream = File.OpenRead (logPath);
		byte[] bytes = new byte[fileStream.Length];
		fileStream.Read (bytes,0,bytes.Length);

		WWWForm form = new WWWForm();
		form.AddField("frameCount", Time.frameCount.ToString());
		form.AddBinaryData("fileUpload", bytes, "log_0.bak1.txt", "image/png");
		WWW w = new WWW(screenShotURL, form);
		yield return w;
		if (!string.IsNullOrEmpty(w.error))
		{
			print(w.error);
		}
		else
		{
			print (w.text);
			print("Finished Uploading Screenshot");
		}

	}
}

参考:http://php.net/manual/zh/

posted on 2016-12-07 16:27  魔天天  阅读(263)  评论(0编辑  收藏  举报