By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

php code to receive image from cellphone

P: 3
The cellphone uses the following C# code to send the image to picinput.php

Expand|Select|Wrap|Line Numbers
  1.               string baseUrl = "" + fileName;
  2.                 httpWebRequest = (HttpWebRequest)WebRequest.Create(baseUrl);
  3.                 httpWebRequest.Timeout = 20000;
  4.                 httpWebRequest.Method = "POST";                
  5.                 httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  6.                 httpWebRequest.ContentLength = imageData.Length;
  8.                 using (Stream oStream = httpWebRequest.GetRequestStream())
  9.                 {
  10.                     oStream.Write(imageData, 0, imageData.Length);
  11.                     oStream.Close();
  12.                 }
What is the most efficient php code to receive this image and write it to a file on the web server?
Apr 1 '10 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 5,058

I'm not sure the request that code creates is valid. A HTTP request that includes binary data is typically of the type "multipart/form-data", not "application/x-www-form-urlencoded".

To quote the HTML4.1 specs:
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
See more details at

When it receives a proper request, PHP simply puts files in the $_FILES array so that you can easily access them in your code.
(See Handling file uploads in the manual.)
Apr 1 '10 #2

P: 3
Thank you for that. As I am not writing the C# part of this, I have sent your comments to the person who is.

My confusion arises in not knowing if I should be using $_POST, $_REQUEST, imagecreatefromfile, file_get_contents, HttpResponse::GetRequestBody or Http::GetREquestBodyStream or something else on the PHP side to capture the incomming data in a form that I can then write it as a file to a folder.
Apr 1 '10 #3

P: 3
Sorry, I should read all your message.

if($_FILES['image']['name']) { etc.

What syntax do I use to get the incomming file into $_FILES?
Apr 1 '10 #4

Expert 5K+
P: 5,058
None. PHP does that automatically. Any file sent as a part of a valid "multipart/form-data" request will be automatically added to the $_FILES array, where you can access them.

For other types of requests, all fields sent in the body of a "application/x-www-form-urlencoded" POST request are added to the $_POST array, and all key-value pairs sent via the URL's query string are added to the $_GET array.
Apr 2 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.