How To Save Image Through Capturing In Android To Php?
Solution 1:
Please try this code this code working for me.
<?php// Path to move uploaded files$target_path = "uploads/";
// array for final json respone$response = array();
// getting server ip address$server_ip = gethostbyname(gethostname());
// final file url that is being uploaded$file_upload_url = 'http://' . $server_ip . '/' . '/xyz/' . '/' . $target_path;
if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);
// reading other post parameters$email = isset($_POST['email']) ? $_POST['email'] : '';
$website = isset($_POST['website']) ? $_POST['website'] : '';
$response['file_name'] = basename($_FILES['image']['name']);
$response['email'] = $email;
$response['website'] = $website;
try {
// Throws exception incase file is not being movedif (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// make error flag true$response['error'] = true;
$response['message'] = 'Could not move the file!';
}
// File successfully uploaded$response['message'] = 'File uploaded successfully!';
$response['error'] = false;
$response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
} catch (Exception$e) {
// Exception occurred. Make error flag true$response['error'] = true;
$response['message'] = $e->getMessage();
}
} else {
// File parameter is missing$response['error'] = true;
$response['message'] = 'Not received any file!F';
}
// Echo final json response to clientecho json_encode($response);
?>
Solution 2:
I'm not and Android developer.
But I know the problem is with your request. You are passing all data in GET
request method (appending parameters in URL). But It has limitation of 2,083 character length. (It depends on webserver actually)
So, If your image size is higher than that, it will simply truncate from URL.
For the solution, you have to use POST
request to submit data to webservice.
Android
Refer answer of this question for android code snippet
PHP code
<?php$json=json_decode($_POST['json']); //decoding json which is received from Android.$photo=$json->images; //get image data (which is encoded in base64)/* Decoding image */$binary = base64_decode ( $photo );
/* Opening image */$file = fopen ( 'file_name_to_create.jpeg', 'wb' );
/* Writing to server */
fwrite ( $file, $binary );
/* Closing image file */
fclose ( $file );
?>
It will create file_name_to_create.jpeg
in current directory.
Reference
Solution 3:
Hi i have used this approach to save myImage.
Create images folder inside your root.
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../images/" . $varFileName);
Use this URL to download my code for sending imagefile to php server.
final Dictionary<String, String> dictionary = new Hashtable<String, String>();
dictionary.put("name", "kruti");
dictionary.put("file", "file");
Post a Comment for "How To Save Image Through Capturing In Android To Php?"