我有一个带3个参数的post方法的php服务器,其中第一个参数用于user_id,第二个参数用于交易ID(trans_id),最后一个参数用于照片文件。如下:
include_once "conn.php";
//Getting the server ip
$server_ip = gethostbyname(gethostname());
//creating the upload url
$upload_url = 'http://'.$server_ip.'/ecommerce/assets/images/user/';
//response array
$response = array();
$username = $_POST['user'];
$trans_id = $_POST['trans_id'];
//getting file info from the request
$fileinfo = pathinfo($_FILES['image']['name']);
//getting the file extension
$extension = '.'.$fileinfo['extension'];
$file_url = $username.time();
$file_path = 'assets/images/komplain/' . $file_url . $extension;
$uploadPath = 'assets/images/komplain/';
$result = array("success" => $_FILES["image"]["name"]);
try{
//saving the file$sourceProperties = getimagesize($fileName);
$fileName = $_FILES['image']['tmp_name'];
$sourceProperties = getimagesize($fileName);
$fileExt = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$uploadImageType = $sourceProperties[2];
$sourceImageWidth = $sourceProperties[0];
$sourceImageHeight = $sourceProperties[1];
switch ($uploadImageType) {
case IMAGETYPE_JPEG:
$resourceType = imagecreatefromjpeg($fileName);
$imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
imagejpeg($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
break;
case IMAGETYPE_GIF:
$resourceType = imagecreatefromgif($fileName);
$imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
imagegif($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
break;
case IMAGETYPE_PNG:
$resourceType = imagecreatefrompng($fileName);
$imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
imagepng($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
break;
default:
$imageProcess = 0;
break;
}
// File successfully uploaded
//adding the path and name to database
if(move_uploaded_file($_FILES['image']['tmp_name'],$file_path)){
$sql = "UPDATE komplain SET foto='$file_url', foto_type='$extension' WHERE user = '$username' AND trans_id=$trans_id";
mysqli_query($conn,$sql);
$response['message'] = 'File uploaded successfully!';
$response['status'] = 'success';
$response['file_path'] = $upload_url."/ecommerce". $file_url . $extension;
}
//if some error occurred
}catch(Exception $e){
$response['status'] = 'fail';
$response['message'] = $e->getMessage();
}
echo mysqli_error($conn);
echo json_encode($response, JSON_PRETTY_PRINT);
function resizeImage($resourceType,$image_width,$image_height) {
$resizeWidth = 250;
$resizeHeight = 250;
$imageLayer = imagecreatetruecolor($resizeWidth,$resizeHeight);
imagecopyresampled($imageLayer,$resourceType,0,0,0,0,$resizeWidth,$resizeHeight, $image_width,$image_height);
return $imageLayer;
}
然后,我使用kotlin作为客户端,并使用MultipartUploadRequest方法将其上传到服务器。如下:
btn_send.setOnClickListener{ uploadMultipart() }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri)
img.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
fun getPath(uri: Uri): String {
var cursor = getContentResolver().query(uri, null, null, null, null)
cursor.moveToFirst()
var document_id = cursor.getString(0)
document_id = document_id.substring(document_id.lastIndexOf(":") + 1)
cursor.close()
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
MediaStore.Images.Media._ID + " = ? ",
arrayOf<String>(document_id),
null
)
cursor.moveToFirst()
val path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
cursor.close()
return path
}
fun uploadMultipart() {
//getting the actual path of the image
val path = getPath(uri!!)
//Uploading code
try {
val uploadId = UUID.randomUUID().toString()
//Creating a multi part request
MultipartUploadRequest(this, uploadId, UPLOAD_URL)
.addFileToUpload(path, "image") //Adding file
.addParameter("user", MCart.user_id) //Adding text parameter to the request
.addParameter("trans_id", trans.toString()) //Adding text parameter to the request
.setNotificationConfig(UploadNotificationConfig())
.setMaxRetries(2)
.startUpload() //Starting the upload
} catch (exc: Exception) {
Toast.makeText(this, exc.message, Toast.LENGTH_SHORT).show()
}
}
我得到的问题是,我只得到了 成功上传到服务器文件夹中,而我的 MySQL的。我输入了在其中创建的trans_id和user_id (.addParameter),但在mysql(数据库)中却什么也没得到