1
首先,讓我描述一下我在這裏面臨的問題。php中未定義的索引圖像
我想存儲從android
到MySQL
的圖像路徑,並將圖像存儲到PhotoUpload目錄中。
點擊提交按鈕後,所有listItem
將被保存到MySQL
。
活動A
public void uploadImageAndText(ArrayList<ImageAndText> listItems, final String id) {
JSONArray jsonArray = new JSONArray();
try {
for (ImageAndText i : listItems) {
JSONObject object = new JSONObject();
String type = i.getType();
String[] Type = type.split(":");
object.put("type", Type[1]);
Toast.makeText(getApplicationContext(), Type[1], Toast.LENGTH_LONG).show();
String amount = i.getAmount();
String[] Amount = amount.split(":");
object.put("amount", Amount[1]);
String description = i.getDescription();
String[] Description = description.split(":");
object.put("description", Description[1]);
Bitmap uploadImage = i.getImage();
String image = getStringImage(uploadImage);
object.put("image", image);
object.put("ts_id", id);
jsonArray.put(object);
}
} catch (JSONException e) {
e.printStackTrace();
}
AddStaff ru = new AddStaff(jsonArray);
ru.execute();
}
class AddStaff extends AsyncTask<String, Void, String> {
ProgressDialog loading;
JSONArray jsonArray;
AddStaff(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(AddClaims.this, "Please Wait", null, true, true);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("listItems", jsonArray.toString());
RequestHandler rh = new RequestHandler();
String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
}
PHP
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!empty($_POST['listItems'])){
$mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
if($mysqli->connect_errno) echo "Failed to connect to MySQL";
$image = $_POST['image'];
$listItems = json_decode($_POST['listItems'], true);
$sql="SELECT id FROM staff_benefit ORDER BY id ASC";
$id=0;
$res=$mysqli->query($sql);
while($rs=$res->fetch_object()) $id=$rs->id;
$path="$id.png";
$actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";
$sql="INSERT INTO `staff_benefit` (`type`, `amount`, `description`, `image`, `ts_id`) VALUES (?, ?, ?, ?, ?)";
$stmt=$mysqli->prepare($sql);
$pathelements=array(realpath($_SERVER['DOCUMENT_ROOT']), 'CRUD', 'PhotoUpload', '');
$savepath = realpath(implode(DIRECTORY_SEPARATOR, $pathelements)) . "{$id}.png";
$bytes=file_put_contents($savepath, base64_decode($image));
if(!$bytes){
echo 'Error saving image';
}
if ($stmt && $bytes) {
foreach($listItems as $item){
$stmt->bind_param('sssss', $item['type'], $item['amount'], $item['description'], $actualpath, $item['ts_id']);
$res=$stmt->execute();
if(!$res) echo 'Query failed with code: '.$stmt->errno;
}
}
$mysqli->close();
}
}
?>
這是我所期待的
錯誤
未定義指數:在C圖像:......錯誤保存圖像
這是行10
$image = $_POST['image'];
我知道我獲取此錯誤消息是因爲我只發佈listItem
但沒有圖像。 我的問題是如何打破listItem中的圖像,然後解碼它?
是否有人可以幫助我瞭解如何ArrayList的存儲(含圖片)到MySQL?多謝
我遵循這個tutorial,但問題是,他存儲的單個文件,ArrayList不是!
'$ _ POST [ '形象']'=>'$ _FILES [ '形象']' –
翻譯一下弗雷德II是說:上傳是$ _FILES數組中,不在$ _POST中。 –
@ Fred-ii-試過,沒有運氣 – Tony