Hello again with my silly question, I got a problem with file upload, here's my code
<form id="uploadAdmin" method="POST" action="updateAdmin.php" novalidate="novalidate">
<div class="mdc-layout-grid__cell stretch-card mdc-layout-grid__cell--span-6">
<img width="140px" src=<?php echo '"../image/logo/'.$_SESSION["logo"].'"'; ?> alt="logo">
</div>
<div class="mdc-layout-grid__cell stretch-card mdc-layout-grid__cell--span-6">
<input type="file" id="logo" name="logo" enctype="multipart/form-data" accept="image/*">
</div>
<br>
<button type="submit" id="buttonUpdateAdmin" class="mdc-button mdc-button--raised mdc-button--compact mdc-ripple-upgraded" data-mdc-auto-init="MDCRipple" style="--mdc-ripple-fg-size:42.6094px; --mdc-ripple-fg-scale:2.10327;">
Upload Logo
</button>
</form>
Here's my PHP code:
$target_dir = "../image/logo/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["logo"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$_SESSION['alert-failure'] = "File yang anda masukkan tidak valid";
header("Location: editProfil.php");
die();
$uploadOk = 0;
}
if ($_FILES["logo"]["size"] > 1024000) {
$_SESSION['alert-failure'] = "Ukuran maximum file yang diperlukan 1 MB";
header("Location: editProfil.php");
die();
$uploadOk = 0;
}
// if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
// && $imageFileType != "gif" ) {
// $_SESSION['alert-failure'] = "File yang dimasukkan tidak valid";
// header("Location: editProfil.php");
// die();
// $uploadOk = 0;
// }
if ($uploadOk == 0) {
$_SESSION['alert-failure'] = "Terjadi kesalahan";
header("Location: editProfil.php");
die();
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
$_SESSION['alert-success'] = "The file ". basename( $_FILES["logo"]["name"]). " has been uploaded.";
header("Location: editProfil.php");
} else {
$_SESSION['alert-failure'] = "Terjadi kesalahan";
header("Location: editProfil.php");
}
}
It expected to upload a file to localhost, specifically image. Maybe I made some mistake, but I didn't notice at all. Your answer would be very helpful to me, thank you.
You need to put the enctype="multipart/form-data" on the form. I don't know if it's necessary on the input.
<form id="uploadAdmin" method="POST" action="updateAdmin.php" enctype="multipart/form-data" novalidate="novalidate">