Change the filename when sending the file from Android to the server


I am writing an application with which a file can be uploaded to a server. During the running of the application, information is written to the file and deleted. Whenever the file is uploaded to the server, I want a new file name so that the old file is not replaced. However, the file can be large and hence I want to maintain only one file on the android system. I want the file name on the server to be having the date and time for when it was uploaded. Is there someway to make the file name on the server to be different from what it is on the device sending it?

I am uploading the file to the server using standard multipart entity framework and php code on the server. The php code is shown below:

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /

<input type="submit" value="Upload File" />

  $to_file = "locationdata/" . basename($_FILES['uploadedfile']['name']);
  $from_file = $_FILES['uploadedfile']['tmp_name'];

  if (move_uploaded_file($from_file, $to_file)) {
    echo "Successful upload";
  <a href="<?php echo $to_file;?>"><?php echo $to_file;?></a>
  } else {
    echo "Unsuccessful upload";

Below are snippets of my code for the android application.

public static File file = new File("/sdcard/upload.txt");
public static FileWriter writer;

This code is in a method I call during the running of the application (possibly multiple times).

writer = new FileWriter(file);
writer.write(".... what I want to print here.......");

And then I send the file to the server with the following code.

HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpPost(webAddress);
MultipartEntity form = new MultipartEntity();
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
form.addPart(inputName, new FileBody(new File(filePath)));
((HttpEntityEnclosingRequestBase) request).setEntity(form);

try {
    System.out.println("Connection Success");
} catch (ClientProtocolException e) {
    throw e;
} catch (IOException ee) {
    throw ee;

Is there someway to make the file name on the server to be different from what it is on the device sending it?

Yes! All you have to do is change the $to_file to something else like:

$dates = date('YmdHis');
$to_file = "locationdata/" . $dates."." . basename($_FILES['uploadedfile']['name']);

edit: added .".". so that there is a period separating the date/time from the original file name. Easier for you to explode it should the need arise later.