Why is_uploaded_file Returns False in Your Java-to-PHP Upload Scenario
You are encountering a classic interoperability issue between Java's HTTP client stack and PHP's file upload handling mechanisms. The symptom is clear: your Java application successfully sends the binary data, evidenced by the 200 OK response and the visible Content-Length header (13091 bytes). However, the PHP script reports is_uploaded_file($_FILES['userfile']['tmp_name']) as false and an empty $_FILES array.
This mismatch occurs because of how the HTTP request is being constructed versus how PHP expects to receive multipart file data. When you inspect the raw request headers generated by your Java code, the payload is sent with a Content-Type: binary/octet-stream. While this conveys raw binary data correctly, it lacks the specific MIME boundaries and form field structure required for PHP's standard upload handler to populate the $_FILES superglobal.
PHP relies on the multipart/form-data content type to parse incoming requests into its internal associative arrays. When you set the entity directly as a stream with Content-Type: binary/octet-stream, PHP treats the entire request body as raw input rather than a structured form submission. Consequently, $_POST and $_FILES remain empty because the parser never invoked the logic to extract file metadata like filename, type, or temporary path.
The discrepancy between Apache HttpClient 4.0 and version 3.1 further highlights this protocol nuance. In HttpClient 3.1, the request might have been accepted even with an incorrect content type, allowing PHP to read the raw stream. However, is_uploaded_file specifically checks for a file uploaded via HTTP POST using the standard multipart mechanism. Since your Java client bypassed this mechanism by sending raw octets, the function correctly returned false, despite the file data physically arriving at the server's socket buffer.
To resolve this, you must align the Java request with PHP's expectations. You need to switch from FileEntity to a StringEntity or MultipartEntity and explicitly set the content type to multipart/form-data. Additionally, the boundary string used for multipart separation must be included in the header value.
Here is how you should refactor the Java code using Jakarta HttpClient (or Apache HttpClient 4.x) to ensure PHP recognizes the upload:
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.util.EntityUtils;
public class PostFile {
public static void main(String[] args) throws Exception {
// Use the modern HttpClient implementation or ApacheHttpClientConnectionManager
HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW"); // Optional, often auto-generated
// Add the file part. Note the name 'userfile' matches the PHP $_FILES key
builder.addBinaryBody("userfile", new java.io.File("c:/TRASH/zaba_1.jpg"));
// Set the correct content type with boundary if not handled automatically by the library version
String contentType = "multipart/form-data; boundary=" + builder.getBoundary();
httppost.setHeader("Content-Type", contentType);
httppost.setEntity(builder.build());
HttpResponse response = new org.apache.http.client.HttpClient().execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
By constructing the entity with MultipartEntityBuilder and naming the body part userfile, you trigger PHP's internal multipart parser. This ensures that $_FILES['userfile'] is populated correctly, allowing is_uploaded_file to return true and move_uploaded_file to execute without triggering a security warning about potential attacks.
If you are maintaining legacy code using older HttpClient versions, ensure you do not manually set Content-Type: binary/octet-stream when uploading files to PHP endpoints. The lack of a boundary string in the header is the primary reason for the silent failure you observed. Always verify that the request headers match the strict requirements of the server-side language's parsing logic.