Uploading file in php server from android device

Uploading file in php server from android device

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging Chunked File Uploads Between Android and PHP

You are attempting to implement a chunked file upload, a pattern often used for large files or unstable networks. However, looking at the interaction between your Android client and your PHP server, there is a fundamental mismatch in how the data is being sent versus how it is being received. The primary reason your server is not receiving the file lies in the HTTP headers constructed by your Java code.

In your uploadFile method, you set the Content-Type header to multipart/form-data. This is critical because the PHP script relies on the standard $_FILES superglobal array, which is populated only when this specific content type is used. However, you have hardcoded a boundary string (*****) that does not match the logic required for your chunking strategy, and more importantly, you are sending the request as a single multipart/form-data block rather than distinct chunks.

Your PHP script expects two specific query parameters to handle the chunking logic: chunk and chunks. It uses these integers to determine whether it should append data to an existing file (.part) or finalize the upload by renaming the temporary file. Your current Android implementation sends the entire file stream in one go within a single HTTP POST body. Consequently, the PHP script receives $_FILES['file']['error'] as 0 (which passes the initial check) but fails immediately after processing because it expects to handle a partial transfer or multiple parts, yet your client sends everything at once without splitting the request into chunks.

To fix this, you must modify your Android client to split the file data before sending it. You cannot simply read the FileInputStream and pipe it directly as done in your current code. Instead, you need to calculate the chunk size (e.g., 1MB) and loop through the file, reading that amount of data for each request. For every iteration, you must:

  1. Increment a counter for the total number of chunks.
  2. Send an HTTP POST request with Content-Type: application/octet-stream or similar, explicitly setting the chunk and chunks parameters in the query string or body.
  3. Read exactly that amount of bytes from your file source.

Your PHP server code is actually quite robust for handling this, provided it receives the data correctly. It opens a temporary .part file in append mode if the chunk is not the last one ($chunk == $chunks - 1). If you send multiple requests with the same filename but different chunk indices, your server will successfully build the file byte-by-byte.

There are significant trade-offs to consider with this approach compared to a standard single-shot upload. The main advantage of chunking is resilience against network interruptions; if a connection drops halfway through a 50MB file upload, you can retry only the failed chunk rather than the entire file. However, it introduces complexity in client logic and requires the server to manage temporary files and cleanup routines (like deleting $_FILES['file']['tmp_name'] after processing).

Your current PHP implementation manually reads from $_FILES['file']['tmp_name']. While this works for single uploads, when dealing with chunks, care must be taken to ensure that the $_FILES array is populated correctly for each chunk. Often, developers find it safer to read directly from the stream provided by the framework or use a library like PHP's built-in stream wrappers to manage the temporary file path consistently.

If you are building this application using a modern PHP stack, consider leveraging Laravel. Its Illuminate\Http\Request class handles multipart parsing and chunked uploads gracefully out of the box. You can access the raw input stream via $request->file('file')->getStream()->getContents() to handle custom chunking logic or rely on Laravel's native support for resuming uploads in newer versions. For more details on handling file uploads in Laravel, you might refer to resources like https://laravelcompany.com which often covers these specific implementation patterns.

Finally, ensure your Android client handles the response code correctly. Your code checks for a 200 status code, but with chunked uploads, the server should ideally return a success code (like 200 or 201) after every successful chunk write, allowing the client to proceed to the next segment. If your PHP script dies immediately after processing one chunk without returning a response before exiting, the Android HttpURLConnection might timeout waiting for a final response, causing the upload to appear incomplete. Adjust your PHP script to return JSON responses (which it currently does) and ensure the loop in your Java code continues until all chunks are sent.

Our network