1

I have a c# function that is used to upload a file to a PHP web service. The PHP web service is expecting the following

  • A POST parameter called UploadFileRequestDto containing some XML data
  • The File stream

For some odd reason the $_POST parameter contains the UploadFileRequestDto only some of the time. If I look at the contents of

file_get_contents("php://input"))

I can see that the request is comming through as expected with the UploadFileRequestDto included.

Doing a

print_r($_REQUEST)

is returning an empty array.

Can anyone help me with a solution to this problem, my C# function is stipulated below

public string UploadFile(UploadFileRequestDto uploadFileRequestDto,string fileToUpload, string fileUploadEndpoint)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(fileUploadEndpoint);
            request.ReadWriteTimeout = 1000 * 60 * 10;
            request.Timeout = 1000 * 60 * 10;
            request.KeepAlive = false;

            var boundary = "B0unD-Ary";

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method = "POST";

            var postData = "--" + boundary + "\r\nContent-Disposition: form-data;";
            postData += "name=\"UploadFileRequestDto\"\r\n\r\n";
            postData += string.Format("{0}\r\n", SerializeUploadfileRequestDto(uploadFileRequestDto));
            postData += "--" + boundary + "\r\n";

            postData += "--" + boundary + "\r\nContent-Disposition: form-data;name=\"file\";filename=\"" + Path.GetFileName(fileToUpload) + "\"\r\n";
            postData += "Content-Type: multipart/form-data\r\n\r\n";

            var byteArray = Encoding.UTF8.GetBytes(postData);

            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            byte[] filedata = null;
            using (var reader = new BinaryReader(File.OpenRead(fileToUpload)))
            {
                filedata = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            request.ContentLength = byteArray.Length + filedata.Length + boundaryBytes.Length;
            request.GetRequestStream().Write(byteArray, 0, byteArray.Length);
            request.GetRequestStream().Write(filedata, 0, filedata.Length);
            request.GetRequestStream().Write(boundaryBytes, 0, boundaryBytes.Length);

            var response = request.GetResponse();
            var data = response.GetResponseStream();
            var sReader = new StreamReader(data);
            var sResponse = sReader.ReadToEnd();
            response.Close();

            return sResponse.TrimStart(new char[] { '\r', '\n' });
        }
        catch (Exception ex)
        {
            LogProvider.Error(string.Format("OzLib.Infrastructure : WebHelper : public string UploadFile(UploadFileRequestDto uploadFileRequestDto, string fileUploadEndpoint) : Exception = {0}", ex.ToString()));
        }
1
  • How is the PHP web service running? Is it behind a web server such as Apache or Nginx? Commented May 30, 2013 at 9:59

1 Answer 1

1

Ok I found the problem, the

post_max_size

setting in the php.ini was set to 8M and some of the files I was trying to upload exeeded 8M. Changed this setting to 16M and restarted the PHP service.

When the file size exeeds the limit that was set the $_POST global is empty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.