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>