I'm trying to upload a file to a remote server using php's fsockopen. If my request looks like:
$httpContent = [
"POST /index.php HTTP/1.1",
"Host: The IP address of my remote server",
"Connection: Close",
"User-Agent: My client"
];
the server responses with 200 OK
. However if I add
"Content-Type: multipart/mixed; boundary=".md5('myBoundary'),
"--".md5('myBoundary'),
"Content-Type: text/plain",
"value",
"--".md5('myBoundary')."--"
The server returns "400 Bad Request". Everything seems fine to me, but somehow it doesn't work. What am I doing wrong?
Any help is greatly appreciated!
Edit (@Rei advised to post a dump of the request):
this is what echoes in client's terminal
POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/mixed; boundary=be0850c82dd4983ddc49a51a797dce49
--be0850c82dd4983ddc49a51a797dce49
Content-Type: text/plain
value
--be0850c82dd4983ddc49a51a797dce49--
And this is what the server gets (caught with tcpdump):
POST /index.php HTTP/1.1
Host: The IP address of my remote server
Connection: Close
User-Agent: My client
Content-Type: multipart/mixed; boundary=be0850c82dd4983ddc49a51a797dce49
--be0850c82dd4983ddc49a51a797dce49
Content-Type: text/plain
value
--be0850c82dd4983ddc49a51a797dce49--
I personally don't see any difference between the two and also don't see anything wrong with the request. Yet, the server returns "400 Bad Request".
*Note: The "The IP address of my remote server" is a real IP address, but I'm not posting it due to security concerns.
**Note: Didn't mention that the PHP script is run in a CLI environment and is not used as a web-application back-end (the requests are NOT prepared by a web browser).
fwrite()
-ing into a variable andecho
it in the end. Alternatively, every time you callfwrite()
, pass the same string toecho
. Make sure that youecho
nothing else or you'll confuse yourself.