181 lines
5.5 KiB
PHP
181 lines
5.5 KiB
PHP
<?php
|
|
session_start();
|
|
require '../minio/vendor/autoload.php';
|
|
|
|
use Aws\S3\S3Client;
|
|
use Aws\S3\Exception\S3Exception;
|
|
|
|
/**
|
|
* Buat koneksi MinIO
|
|
*/
|
|
function minioClient() {
|
|
return new S3Client([
|
|
'version' => 'latest',
|
|
'region' => 'us-east-1',
|
|
'endpoint' => 'https://storage.manjapro.net',
|
|
'use_path_style_endpoint' => true,
|
|
'credentials' => [
|
|
'key' => 'admin',
|
|
'secret' => '@P4ssw0rd',
|
|
],
|
|
'suppress_php_deprecation_warning' => true,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Upload file ke MinIO
|
|
*/
|
|
function minioUpload($bucket, $key, $sourceFile) {
|
|
$s3 = minioClient();
|
|
|
|
try {
|
|
$result = $s3->putObject([
|
|
'Bucket' => $bucket,
|
|
'Key' => $key,
|
|
'SourceFile' => $sourceFile,
|
|
'ACL' => 'public-read', // opsional
|
|
]);
|
|
return $result['ObjectURL'];
|
|
} catch (S3Exception $e) {
|
|
error_log("MinIO Upload Error: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Upload lokal dan MinIO
|
|
*/
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
|
|
|
|
session_start(); // pastikan session aktif
|
|
|
|
$fileExt = "." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
$fileName = "bilspro_" . date('YmdHis') . $fileExt;
|
|
$formId = $_POST['formId'];
|
|
|
|
// --- 📤 Upload langsung ke MinIO ---
|
|
$tmpFilePath = $_FILES['file']['tmp_name']; // ambil file temporary upload
|
|
|
|
// 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] = [
|
|
// 'file_name' => $fileName,
|
|
// 'minio_url' => $minioUrl
|
|
// ];
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => 'File berhasil diupload.',
|
|
'file_name' => $fileName,
|
|
'minio_url' => $minioUrl
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Gagal upload.'
|
|
]);
|
|
}
|
|
|
|
} else {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Tidak ada file diupload atau terjadi error.'
|
|
]);
|
|
}
|
|
} else {
|
|
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.']);
|
|
// }
|
|
|
|
|
|
|
|
// session_start();
|
|
|
|
// if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
|
|
// $uploadDir = '../img/user/';
|
|
// $fileExt = ".".pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
|
|
// $fileName = "bilspro_".date('YmdHis').$fileExt;
|
|
// $uploadFile = $uploadDir . $fileName;
|
|
// $formId = $_POST['formId'];
|
|
|
|
// if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
|
|
// // Menyimpan nama file dalam variabel session
|
|
// if (!isset($_SESSION['uploaded_files'])) {
|
|
// $_SESSION['uploaded_files'] = [];
|
|
// }
|
|
// $_SESSION['uploaded_files'][$formId] = $fileName;
|
|
|
|
// echo 'File successfully uploaded.';
|
|
// } else {
|
|
// echo 'Error uploading file.';
|
|
// }
|
|
// } else {
|
|
// echo 'No file uploaded or upload error.';
|
|
// }
|
|
// } else {
|
|
// echo 'Invalid request method.';
|
|
// }
|
|
?>
|