Skip to content

Instantly share code, notes, and snippets.

@raykibul
Created February 19, 2021 13:28
Show Gist options
  • Select an option

  • Save raykibul/86470c759e482825c99a4d62512dd18c to your computer and use it in GitHub Desktop.

Select an option

Save raykibul/86470c759e482825c99a4d62512dd18c to your computer and use it in GitHub Desktop.
private void UploadProfilePicture(Uri imageUri){
if (imageUri!=null){
File file=new File(imageUri.getPath());
if (file==null)
return;
RequestBody requestUserId=RequestBody.create(MediaType.parse("multipart/form-data"),reseller.getUsername());
RequestBody requestKey=RequestBody.create(MediaType.parse("multipart/form-data"),reseller.getPassword());
RequestBody requestImage =RequestBody.create(MediaType.parse("multipart/form-data"),file);
MultipartBody.Part imagePart = null;
imagePart=MultipartBody.Part.createFormData("fileToUpload",file.getName(),requestImage);
Call<ResponseBody>uploadProfile= controller.getService().uploadProfilepic(imagePart,requestUserId,requestKey);
uploadProfile.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (!response.isSuccessful()){
Toast.makeText(MainActivity.this, "Server Problem : code "+response.code(), Toast.LENGTH_SHORT).show();
return;
}
try {
Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "Some Thing went wrong!", Toast.LENGTH_SHORT).show();
}
});
}else
Toast.makeText(this, "Image File Not found!", Toast.LENGTH_SHORT).show();
}
<?php
session_start();
require_once 'autoload.php';
$user = Xss::xssClean(Input::get('user'));
$key = Xss::xssClean(Input::get('key'));
if(!isset($user) || empty($user)){
echo 'ERROR : INVALID REQUEST';
exit();
}
if(!isset($key) || empty($key)){
echo 'ERROR : INVALID REQUEST';
exit();
}
$verifyUserSql = "SELECT * FROM reseller WHERE username = ? LIMIT 1";
$userVerifyUser = DB::getInstance()->get($verifyUserSql, array($user));
$userCount = $userVerifyUser->Count();
if ($userCount > 0) {
$loginSql = "SELECT * FROM reseller WHERE username = ? AND password = ? AND status = 1 LIMIT 1";
$loginQuery = DB::getInstance()->get($loginSql, array($user, $key));
$loginCount = $loginQuery->Count();
if ($loginCount > 0) {
$loginResults = $loginQuery->result();
foreach ($loginResults as $login) {
$id = $login->id;
$userType = $login->custype;
$name = $login->name;
$userName = $login->username;
$pin = $login->pincode;
}
Session::set('pin',$pin);
Session::set('id',$id);
Session::set('userName', $userName);
} else {
echo 'ERROR : INVALID USER';
exit();
}
} else {
echo 'ERROR : INVALID USER';
exit();
}
$target_dir = "../content/profile_images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
exit();
}
if ($_FILES["fileToUpload"]["size"] > 1000000) {
echo "Sorry, your file is too large.max size: 1mb";
$uploadOk = 0;
exit();
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
exit();
}
$newfileName='khantelecomimageofuserthisklasdjfkasdflk'.Session::get('id');
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir.$newfileName)) {
echo "Your Profile has been uploaded.";
exit();
} else {
echo "Sorry, there was an error uploading your file.";
exit();
}
?>
@Multipart
@POST("/api/profilepic.php")
Call<ResponseBody> uploadProfilepic(
@Part MultipartBody.Part filefileToUpload,
@Part("user") RequestBody user,
@Part("key") RequestBody key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment