Help for this specific server side php code I don\'t have any knowledge of php and I have to upload three images from android to this php page.
I have tried many met
Looks like your PHP is correct.
On your device use HTTP POST request with MultipartEntity
data type. Read more here
EDIT
Example from my link:
You will have to download additional libraries to get MultipartEntity
running!
1) Download httpcomponents-client-4.1.zip from http://james.apache.org/download.cgi#Apache_Mime4J and add apache-mime4j-0.6.1.jar to your project.
2) Download httpcomponents-client-4.1-bin.zip from http://hc.apache.org/downloads.cgi and add httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar to your project.
3) Use the example code below.
private DefaultHttpClient mHttpClient;
public ServerCommunication() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
}
public void uploadUserPhoto(File image1, File image2, File image3) {
try {
HttpPost httppost = new HttpPost("some url");
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Title", new StringBody("Title"));
multipartEntity.addPart("Nick", new StringBody("Nick"));
multipartEntity.addPart("Email", new StringBody("Email"));
multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
multipartEntity.addPart("file1", new FileBody(image1));
multipartEntity.addPart("file2", new FileBody(image2));
multipartEntity.addPart("file3", new FileBody(image3));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
} catch (Exception e) {
Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e);
}
}
private class PhotoUploadResponseHandler implements ResponseHandler {
@Override
public Object handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}
First of all your code could be optimized to the following:
$files = array('file1', 'file2', 'file3');
$path = 'elp/pendingimages/';
foreach ($files as $file) {
if ($_FILES[$file]['error'] > 0) {
echo 'Error: '. $_FILES[$file]['error'] .'<br />';
}
else {
echo 'Upload: '. $_FILES[$file]['name'] .'<br />';
echo 'Type: '. $_FILES[$file]['type'] .'<br />';
echo 'Size: '. ($_FILES[$file]['size'] / 1024) .' Kb<br />';
echo 'Stored in: '. $_FILES[$file]['tmp_name'] .'<br />';
}
$basename = basename($_FILES[$file]['name']);
if (move_uploaded_file($_FILES[$file]['tmp_name'], $path . $basename) {
echo "The file {$basename} has been uploaded";
}
else {
echo 'There was an error uploading the file, please try again!';
}
}
If you're using different field for each file then it's fine.
Next you can see what $_FILES array store in itself when it's multiple upload:
$_FILES = array(
['files'] => array(
['name'] => array(
[0] => 'WALL_video.jpg'
[1] => 'WALLc.jpg'
)
['type'] => array(
[0] => 'image/jpeg'
[1] => 'image/jpeg'
)
['tmp_name'] => array(
[0] => '/tmp/phpnbKcdM'
[1] => '/tmp/phpnrHSN1'
)
['error'] => array(
[0] => 0
[1] => 0
)
['size'] => array(
[0] => 885968
[1] => 839713
)
)
)
The following code will work for you if you're using one field with name like files[]
as array of files.
$target_path = 'elp/pendingimages/';
foreach ($_FILES['files']['name'] as $index => $name) {
if ($_FILES['files']['error'][$index] > 0) {
echo 'Error: ' . $_FILES['files']['error'][$index] . '<br />';
}
else {
echo 'Upload: '. $_FILES['files']['name'][$index] .'<br />';
echo 'Type: '. $_FILES['files']['type'][$index] .'<br />';
echo 'Size: '. ($_FILES['files']['size'][$index] / 1024) .' Kb<br />';
echo 'Stored in: '. $_FILES['files']['tmp_name'][$index] .'<br />';
}
$path = $target_path . basename($name);
if (move_uploaded_file($_FILES['files']['tmp_name'][$index], $path) {
echo "The file {$name} has been uploaded";
}
else {
echo 'There was an error uploading the file, please try again!';
}
}
Check my post on sending multiple images to server Click here