-2

Possible Duplicate:
Headers already sent by PHP

I'm trying to upload a binary file from Java application and handle it through php script at server side.

But it shows following log when I run code in Eclipse (for Java).

Although I'm sending header information in first line, why does it displays this message.

Here is my php code:

<?php
header('Content-type: application/xml');

/*******************************************/
$DIRECTORY_SEPARATOR = "/";
$BUF_SIZE = 4096;

/*******************************************/
uploadFile();
sendResponse();

/*******************************************/
function uploadFile()
{
    global $DIRECTORY_SEPARATOR;
    global $BUF_SIZE;
    $targetDir = "." . $DIRECTORY_SEPARATOR . "uploaded_file";

    /***************************************/
    // 5 minutes execution time
    @set_time_limit(5 * 60);

    /***************************************/
    // Set filename
    $filename="temp_aa.mp3";

    /***************************************/
    if (!file_exists($targetDir))
    {
        mkdir($targetDir,0777,true);
    }

    /***************************************/
    $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
    if ($out) 
    {
        $in = fopen("php://input", "rb");
        if ($in) 
        {
            while ($buff = fread($in, $BUF_SIZE))
            {
                fwrite($out, $buff);
            }

            fclose($in);
        } 
        else
        {
            fclose($out);
            return ;
        }
    } 
    else
    {
        return ;
    }
}

function sendResponse()
{
    echo "
    <?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <data>
      <status type=\"file_upload\">
            <value>ok</value>
      </status>
    </data>
    ";
}

?>

And here is my Java-side code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class TestHttp 
{
    public static void main(String[] args)
    {
        HttpURLConnection httpUrlConnection = null;
        File mFileToUpload = new File("c:/aa.mp3");
        int byteTrasferred = 0, mBufferSize = 4096, mTempBytesRead = 0;
        byte[] mBuffer = new byte[mBufferSize];

        try
        {
            httpUrlConnection = (HttpURLConnection)new URL("http://127.0.0.1/testhttpfileupload3.php").openConnection();
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");
            OutputStream os = httpUrlConnection.getOutputStream();
            Thread.sleep(1000);
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(mFileToUpload));

            while((mTempBytesRead = fis.read(mBuffer)) != -1) 
            {
                os.write(mBuffer, 0, mTempBytesRead);

                byteTrasferred += mTempBytesRead;
                System.out.println("byteTrasferred = " + byteTrasferred);
            }

            fis.close();
            os.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        try
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));

            String s = null;
            while ((s = in.readLine()) != null) 
            {
                System.out.println(s);
            }
            in.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

And here is the error log:

byte Transferred = 8802304

byte Transferred = 8804659
Warning: Unknown: POST Content-Length of 8804659 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0

Warning: Cannot modify header information - headers already sent in C:\wamp\www\testhttpfileupload3.php on line 2

Notice: Undefined variable: fileName in C:\wamp\www\testhttpfileupload3.php on line 34

Notice: Undefined variable: chunk in C:\wamp\www\testhttpfileupload3.php on line 34

Warning: fopen(./uploaded_file) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\testhttpfileupload3.php on line 34

    <?xml version="1.0" encoding="UTF-8"?>
    <data>
      <status type="file_upload">
            <value>ok</value>
      </status>
    </data>
Community
  • 1
  • 1
Nitin Bansal
  • 2,986
  • 3
  • 23
  • 30

2 Answers2

2

The warning message that is written out causes the initial http headers to get send. The header command you're giving afterwards does not work anymore since you already outputted that warning.

Solution is to either change the post upload limit, or disable display_errors in php.ini

cweiske
  • 30,033
  • 14
  • 133
  • 194
2

It looks like you are experiencing a PHP startup error, which is output on the imaginary line 0 (before any of your code is executed).

The warning Unknown: POST Content-Length of 8804659 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 is being output before the header call so it prevents that from working.

You need to either increase the maximum allowed sizes, use the hidden MAX_FILE_SIZE input field 1, or disable startup errors in php.ini (display_startup_errors). In production error display should be turned off anyway. But the main problem is that you are trying to upload a file bigger than PHP is set to allow, so even without that error, the file upload would be incomplete.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • thnx sooo much...i guess you are right....the warning message is being sent as initial headers... – Nitin Bansal Jun 17 '12 at 07:30
  • startup errors are uncommon, but when they do occur, they happen before any of your code is executed, which is what is happening here. glad to help. – drew010 Jun 17 '12 at 07:32