update cara upload gambar
This commit is contained in:
+71
-16
@@ -46,53 +46,108 @@ function minioUpload($bucket, $key, $sourceFile) {
|
||||
/**
|
||||
* Upload lokal dan MinIO
|
||||
*/
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
|
||||
|
||||
$uploadDir = '../img/user/';
|
||||
if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
|
||||
session_start(); // pastikan session aktif
|
||||
|
||||
$fileExt = "." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
||||
$fileName = "bilspro_" . date('YmdHis') . $fileExt;
|
||||
$uploadFile = $uploadDir . $fileName;
|
||||
$formId = $_POST['formId'];
|
||||
|
||||
// --- 1️⃣ Upload ke server lokal ---
|
||||
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
|
||||
// --- 📤 Upload langsung ke MinIO ---
|
||||
$tmpFilePath = $_FILES['file']['tmp_name']; // ambil file temporary upload
|
||||
|
||||
// --- 2️⃣ Upload ke MinIO ---
|
||||
$minioUrl = minioUpload('files', 'user/' . $fileName, $uploadFile);
|
||||
// Pastikan minioUpload menerima path file lokal sementara ($tmpFilePath)
|
||||
$minioUrl = minioUpload('files', 'user/' . $fileName, $tmpFilePath);
|
||||
|
||||
if ($minioUrl) {
|
||||
// Simpan info file ke session
|
||||
if (!isset($_SESSION['uploaded_files'])) {
|
||||
$_SESSION['uploaded_files'] = [];
|
||||
}
|
||||
|
||||
$_SESSION['uploaded_files'][$formId] = $fileName;
|
||||
|
||||
// $_SESSION['uploaded_files'][$formId] = [
|
||||
// 'local' => $fileName,
|
||||
// 'minio' => $minioUrl ?: null
|
||||
// 'file_name' => $fileName,
|
||||
// 'minio_url' => $minioUrl
|
||||
// ];
|
||||
|
||||
// --- Respons sukses ---
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'File berhasil diupload.',
|
||||
'local_path' => $uploadFile,
|
||||
'file_name' => $fileName,
|
||||
'minio_url' => $minioUrl
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal upload ke server lokal.']);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal upload.'
|
||||
]);
|
||||
}
|
||||
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Tidak ada file diupload atau terjadi error.']);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Tidak ada file diupload atau terjadi error.'
|
||||
]);
|
||||
}
|
||||
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Invalid request method.'
|
||||
]);
|
||||
}
|
||||
|
||||
// if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
// if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
|
||||
|
||||
// $uploadDir = '../img/user/';
|
||||
// if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
|
||||
|
||||
// $fileExt = "." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
||||
// $fileName = "bilspro_" . date('YmdHis') . $fileExt;
|
||||
// $uploadFile = $uploadDir . $fileName;
|
||||
// $formId = $_POST['formId'];
|
||||
|
||||
// // --- 1️⃣ Upload ke server lokal ---
|
||||
// if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
|
||||
|
||||
// // --- 2️⃣ Upload ke MinIO ---
|
||||
// $minioUrl = minioUpload('files', 'user/' . $fileName, $uploadFile);
|
||||
|
||||
// // Simpan info file ke session
|
||||
// if (!isset($_SESSION['uploaded_files'])) {
|
||||
// $_SESSION['uploaded_files'] = [];
|
||||
// }
|
||||
|
||||
// $_SESSION['uploaded_files'][$formId] = $fileName;
|
||||
// // $_SESSION['uploaded_files'][$formId] = [
|
||||
// // 'local' => $fileName,
|
||||
// // 'minio' => $minioUrl ?: null
|
||||
// // ];
|
||||
|
||||
// // --- Respons sukses ---
|
||||
// echo json_encode([
|
||||
// 'status' => 'success',
|
||||
// 'message' => 'File berhasil diupload.',
|
||||
// 'local_path' => $uploadFile,
|
||||
// 'minio_url' => $minioUrl
|
||||
// ]);
|
||||
// } else {
|
||||
// echo json_encode(['status' => 'error', 'message' => 'Gagal upload ke server lokal.']);
|
||||
// }
|
||||
|
||||
// } else {
|
||||
// echo json_encode(['status' => 'error', 'message' => 'Tidak ada file diupload atau terjadi error.']);
|
||||
// }
|
||||
|
||||
// } else {
|
||||
// echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user