I'm trying to upload PDF files to a server using PHP in Android. For that I get the URI of the selected file and POST those data to a PHP file in my server. This is where I post that data:
URL url = new URL(url_path);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data");
OutputStream outputStream = conn.getOutputStream();
InputStream inputStream = c.getContentResolver().openInputStream(path);
Log.e("URI",path.toString());
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = inputStream.read(buffer, 0, bufferSize)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
Log.e("URI",String.valueOf(bytesAvailable));
outputStream.flush();
inputStream.close();
This data is posted to this php file:
<?php
print_r($_REQUEST);
$file_name = date("U").".pdf";
$server_path = "uploads/";
$web_path = "http://mysite/uploads/";
$file = $server_path.$file_name;
file_put_contents($file,"");
$fp = fopen("php://input",'r');
echo 'fp : '.$fp;
while ($buffer = fread($fp,8192)){
echo $buffer;
file_put_contents($file,$buffer,FILE_APPEND | LOCK_EX);
}?>
For each upload, it creates a new file in my uploads directory. But the size of the PDF is 0 bytes. I have no idea why. Please help me on this. Thanks in advance.