Php 如何将图像上传到现有的MySql数据库?
我正在创建一个android应用程序,并试图为用户存储配置文件图像。当用户注册时,他被记录在表中,我在那个里又插入了一个字段Php 如何将图像上传到现有的MySql数据库?,php,android,mysql,image,Php,Android,Mysql,Image,我正在创建一个android应用程序,并试图为用户存储配置文件图像。当用户注册时,他被记录在表中,我在那个里又插入了一个字段image,我将在那个里为他存储图像。但我想在发送查询时需要使用PUT请求方法。我上传的图像基于用户id和每个用户注册时获得的认证密钥api。这就是我到目前为止所做的: public function uploadImage($user_id, $image, $status) { $path = "uploads/$user_id.png"; $actua
image
,我将在那个里为他存储图像。但我想在发送查询时需要使用PUT请求方法。我上传的图像基于用户id和每个用户注册时获得的认证密钥api。这就是我到目前为止所做的:
public function uploadImage($user_id, $image, $status) {
$path = "uploads/$user_id.png";
$actualpath = "http://localhost:8081/timster/uploads/$path";
$stmt = $this->conn->prepare("UPDATE users SET image = $actualpath, status = ? WHERE id = ?");
$stmt->bind_param("sii", $image, $status, $id);
$stmt->execute();
$num_affected_rows = $stmt->affected_rows;
$stmt->close();
return $num_affected_rows > 0;
}
我真的被卡住了,我不知道下一步该怎么办。
这就是我发送查询以更新users
表和上传图像的方式
$app->put('/image/:id', 'authenticate', function($user_id) use ($app) {
// check for required params
verifyRequiredParams(array('image', 'status'));
global $user_id;
$image = $app->request->put('image');
$status = $app->request->put('status');
$db = new DbHandler();
$response = array();
$result = $db->uploadImage($user_id, $image, $status);
if ($result) {
$response["error"] = false;
$response["message"] = "Image uploaded successfully";
} else {
$response["error"] = true;
$response["message"] = "Image failed to upload";
}
echoRespnse(200, $response);
});
我运行这个函数并不是为了检查它是否工作,但我确信我还需要一些东西来添加到函数uploadImage()
例如,这就是我的url在发送PUT请求时的外观。
http://localhost:8081/timster/v1/image/2
。最后一个数字是我要上载新图像的用户的id 这里是我很久以前开发的一些代码,用于将图像上传到PHP服务器,希望对您有所帮助
创建php服务器端的第一步:
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['filename'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under './uplodedimages' folder
$file = fopen('uploadedimages/'.$filename, 'wb');
// Create File
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete, Please check your php file directory';
?>
第三步调用IntentService:
Intent intent = new Intent(this, SendImageServer.class);
// add info for the service which file to download and where to store
intent.putExtra(Constants.INTENT_SERVICE_FILE_PATH, mFilePath);
startService(intent);
第四步创建广播接收器,以通信上传结果:
private BroadcastReceiver mReceiverPhoto = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(mProgDialog.isShowing())
{
mProgDialog.dismiss();
}
if (bundle != null) {
String mUrl = bundle.getString(Constants.INTENT_SERVICE_PHOTO);
int resultCode = bundle.getInt(Constants.INTENT_SERVICE_RESULT);
int statusCode = bundle.getInt(Constants.INTENT_SERVICE_STATUS_CODE);
if (resultCode == Constants.RESULT_OK) {
mUser.setmProfilePhoto(mUrl);
((NewUserActivity) context).setPhoto(mUrl);
}
else if (resultCode == Constants.RESULT_FAIL_404) {
Toast.makeText(getApplicationContext(),
getString(R.string.constants_resource_not_found_error),
Toast.LENGTH_LONG).show();
}
else if (resultCode == Constants.RESULT_FAIL_500) {
Toast.makeText(getApplicationContext(),
getString(R.string.constants_server_error),
Toast.LENGTH_LONG).show();
}
else if (resultCode == Constants.RESULT_FAIL) {
Toast.makeText(
getApplicationContext(),
getString(R.string.constants_generic_http_error)
+ statusCode, Toast.LENGTH_LONG)
.show();
}else {
Toast.makeText(NewUserActivity.this, getString(R.string.constants_download_failed),
Toast.LENGTH_LONG).show();
}
}
}
};
第五步注册您的广播接收器:
registerReceiver(mReceiverPhoto, new IntentFilter(Constants.NOTIFICATION));
当您不再需要图像时,不要忘记取消注册。您的图像是否正在上载到上载文件夹,因为我在你的代码中没有看到任何文件上传代码。应该是。我应该在php代码中做一些更改吗?旁注:一个用户应该能够为其他用户上传吗?如果没有Id,跳过URL中的用户Id,只使用令牌中的用户Id,而不是用户登录时,我将从mysql存储数据,如Id、名称、电子邮件到SqLite,稍后检索这些数据,并使用从SqLite检索到的Id发送PUT请求。您好,感谢您抽出时间撰写本文,但我只需要服务器端代码。另外,我要上传的图像需要存储在已经存在一条用户记录的表中。因此,我只需要用图像更新记录。我按照您的需要开发了一些东西,将用户图像和url保存在数据库中的文件夹中,因为以后使用毕加索可以更容易地获取图像并将其显示在我的应用程序中,而不是将用户图像存储在数据库中。我认为这不是你需要的方法,很抱歉我误解了你的问题。你的方法真的很好,我也考虑过。在数据库中存储图像的url。但我将从应用程序发送什么类型的数据。我的意思是,当用户从gallery中选择某个图像时,例如,我将如何将该图像发送到数据库并仅存储图像的url?我的意思是,这一切的过程是什么。我看到您在将图像发送到数据库后进行了一些解码。因为我需要压缩并创建一个字节数组以发送到服务器
registerReceiver(mReceiverPhoto, new IntentFilter(Constants.NOTIFICATION));