I am trying to post a file to a Java servlet, but I am running into some problems on how to get this working properly.
I have an HTML form where the user enters some data. When the user clicks on the submit button, my PHP script is called, which takes the user's input and creates a text file that I want to send to the servlet.
The servlet takes a single variable, the file itself. Here is what I'm trying to do in my PHP script:
$socket = fsockopen("myserver.com", "8080", $errno, $errstr, 30);
if(!$socket) {
echo "$errstr ($errno)<BR>\n";
} else {
$fn = fopen("myfile.txt" , "r");
$fsize = filesize($filename);
fputs($socket, "POST /servlet/pull HTTP/1.0\r\n");
fputs($socket, "Connection: Keep-Alive\r\n");
fputs($socket, "Content-Type: multipart/form-data\r\n");
fputs($socket, "-------------------------------146931364513459\r\n");
fputs($socket, 'Content-Disposition: form-data; name="file"\r\n');
fputs($socket, "Content-type: text/plain");
fputs($socket, fread($fn, $fsize));
fputs($socket, "\r\n-------------------------------146931364513459--\r\n\r\n");
while (!feof($socket)) {
echo fgets($socket,128);
}
fclose($socket);
}
This isn't working, and I have no idea what I'm doing wrong. I've tried using the post.php script to no avail. I'm stuck!
All I want to do is to be able to submit my file to the servlet, just as if I had selected the file using the INPUT TYPE="file" tag from a web page.
Please help! Thanks!
-adam