I want to upload a variable number (user defined) number of files through retro fit to php (photos in this case) to use in my php code. I believe I have the retrofit code but I'm not sure how to do the php/slim code and I can't seem to find an answer. I have searched.
So my android java is something like this in thinking. To upload the variable number of files from the phone to the server.
caption[] = (cap1, cap2, cap3);
Uri[] = (user selected Uri, second uri, third uri, .....as selected);
x = Uri[].length;
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
File file = FileUtils.getFile(this, fileUri);
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getContentResolver().getType(fileUri)),
file
);
return MultipartBody.Part.createFormData(partName, file.getName(),
requestFile);
}
List < MultipartBody.Part > parts = new ArrayList < > ();
While(x >= 0) {
photo = "photo" + x;
parts.add(prepareFilePart(photo, Uri[x]));
x = x - 1;
}
...
Call < ResponseBody > call = service.uploadMultipleFilesDynamic(description, parts);
with a call of
Call<ResponseBody> uploadMultipleFilesDynamic(
@Field("cap[]") ArrayList<String> capation[],
@Part List<MultipartBody.Part> files);
}
Now the php is where I have some real questions as I don't know how to receive this.
Currently for 1 file I do something like this in slim.
$response = array();
if (isset($_POST['cap']) &&
($_POST['IDs']) &&
($_POST['filenamev']) &&
$_FILES['image']['error'] === UPLOAD_ERR_OK) {
$upload = new pictureuploads();
$filep = $_FILES['image']['tmp_name'];
$capp = $_POST['cap'];
$ID = $_POST['IDs'];
$desc = $_POST['filenamev'];
if ($upload->savecapFile($filep, getFileExtension($_FILES['image']['name']), $desc, $ID, $capp)) {
$response['error'] = false;
$response['message'] = 'File Uploaded Successfullly';
} else {
$response['error'] = true;
$response['message'] = 'Required parameters are not available';
}
echo json_encode($response);
}
with the method being used as.
public function savecapFile($filep, $extension, $desc, $ID, $timep)
{
move_uploaded_file($filep, $filedest);
}
So my question is, how do I accept the variable number of files in the php?
The results I would like are simple.
A random number of user defined files gets uploaded. Right now I have to have a 1 for 1 link in my receiving files. I would like to not have to worry about that so the user can upload as many as they choose.