- Home /
Writing an array to a txt document and send it to my server
Hi, im trying to write an array to a txt file (Done succesfully) and then im trying to send the text file to my server via php.
this is my current code for doing this:
IEnumerator UploadFileCo (string uploadURL)
{
WWW localFile = new WWW ("file:///datacolores.txt");
yield return localFile;
if (!(localFile == null)) {
Debug.Log ("Good");
} else {
Debug.Log ("NULL");
}
WWWForm postForm = new WWWForm ();
postForm.AddBinaryData ("file", localFile.bytes, "datacolores.txt", "text/plain");
WWW upload = new WWW (uploadURL, postForm);
yield return upload;
if (upload.error == null) {
Debug.Log (upload.text);
Debug.Log ("upload error null");
} else {
Debug.Log ("Error: " + upload.error);
Debug.Log ("isdone: " + upload.isDone);
Debug.Log ("bytesdownloaded " + upload.bytesDownloaded);
Debug.Log ("progress " + upload.progress);
Debug.Log ("url " + upload.url);
Debug.Log ("responseheaders " + upload.responseHeaders);
Debug.Log ("size " + upload.size);
Debug.Log ("text " + upload.text);
Debug.Log ("uploadprogress " + upload.uploadProgress);
}
}
void UploadFile (string uploadURL)
{
StartCoroutine (UploadFileCo (uploadURL));
}
} and: UploadFile ("http://myserver.com/upload.php");
i get this error: 400 Bad Request
Can anyone point me to the right direction?
This is not a question related to Unity, but with C#. Question rejected
c# is the program$$anonymous$$g base of unity! are you mentaly disabled or how can you say c# is not related to unity?
Unity answers is a Question / Answer site about Unity. If you want help about a question which is only C# related, ask on a C# Question / Answer site
$$anonymous$$oderators guideline
Reasons for a question to get rejected in the mod queue
Non-Unity question: we want to strictly keep Unity Answers for Unity-related content.
yes, but the WWW class is a class from UnityEngine right? and im having trouble with that class as you can see in the description of my question
Have you checked that localFile.bytes is not null?
Also have you tested upload.php outside of unity as a sanity check?
Yes i doubled checked and localFile.bytes is not null
and yes i tested different upload.php files.
if it helps my upload.text debug:
text
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.
UnityEngine.Debug:Log(Object) c__Iterator0:$$anonymous$$oveNext() (at Assets/HACERCUADROS.cs:140)
You need to get a better understanding of how the htt(p) protocol works. If Unity gets a 400 Bad Request it's either a problem on your server or you send the wrong request that your server doesn't understand. In both cases it's related to your server / server configuration and not the WWW class. Are you sure:
your server looks for an URL encoded field called "file"?
it expects a post request for the upload.
you use the correct protocol in your url? Some servers are configured to only accept "https" requests.
if it's a freehoster, make sure the webserver actually allows uploads at all.
serverside script is able to process the file correctly? For example if it is supposed to store that file on the server with the file name "datacolores.txt", are you sure the file doesn't exist yet?
You keep bumping your question without adding anything useful. We can't tell you what's wrong with your setup since the problem is on the server. If you get an actual response from the server that means the connection was successful and the server received your request but was unable to handle it, for whatever reason.
No one will be able to "answer" this question since it's just lacking information. From what we see you're not doing anything wrong in general. However as said it depend on your server side script and server configuration. $$anonymous$$aybe it expects some kind of security cookie?
than you very much for your reply,
as i said before i tested a lot of php files and im sure the php files are correct, im NOT using a freehoster, and im sure there is no datacoleres.txt file in my server. the onlything im not sure is about the correct protocol, you say some servers are configured to only accept https requests, do you have any idea how can i check this? and how can i help for giving more information, what do you need? Sorry for my horrible english and again thankyou very much for your reply!! Hope you can still help me.
Answer by nre3d · Feb 12, 2018 at 09:33 PM
This example worked fine for me. https://forum.unity.com/threads/need-help-with-scripting-php-for-uploading-txt-files-to-http-server.466039/
Your answer
Follow this Question
Related Questions
How would I use a server to constantly check time to prevent cheating? (Energy System) 4 Answers
Help with saving XML files with PHP to a server 0 Answers
Retrieving online highscores (following a guide) 1 Answer
Send an integer array through WWWForm 1 Answer
WWW form isn't working in Build 1 Answer