在尝试将图像上传到服务器时如何解决未定义的索引错误?我正在尝试创建一个API,使用PHP和MySQL将图像上传到服务器
错误:
本节中的注意:未定义索引:img_logo in 第6行的/opt/lampp/htdocs/Carekkerje/upload.php {"成功":0,"消息":"错误上传图片"}
错误:
$ img_logo = $ _POST [' img_logo'];
我的代码:
upload.php
<?php
include_once "koneksi.php";
class emp{}
$img_logo = $_POST['img_logo'];
$random = random_word(20);
$path = "images/".$random.".png";
// sesuiakan ip address laptop/pc atau URL server
$actualpath = "http://192.168.43.193/Carekkerje/$path";
$query = mysqli_query($con, "INSERT INTO transaksi (img_logo) VALUES ('$actualpath')");
if ($query){
file_put_contents($path,base64_decode($img_logo));
$response = new emp();
$response->success = 1;
$response->message = "Successfully Uploaded";
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error Upload image";
die(json_encode($response));
}
// fungsi random string pada gambar untuk menghindari nama file yang sama
function random_word($id = 20){
$pool = '1234567890abcdefghijkmnpqrstuvwxyz';
$word = '';
for ($i = 0; $i < $id; $i++){
$word .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
return $word;
}
mysqli_close($con);
?>