duelfi_api/upload_profile_picture.php
2025-04-11 05:02:06 +08:00

45 lines
1.9 KiB
PHP

<?php
// upload_profile_picture.php
// Set temporary directory for file uploads if not already set
ini_set('upload_tmp_dir', '/var/www/html/duelfi/tmp');
// Check if file is uploaded and privy_id is provided
if ($_FILES['file']['error'] == UPLOAD_ERR_OK && isset($_POST['privy_id'])) {
$privy_id = $_POST['privy_id']; // Retrieve privy_id from POST request
$uploadDir = '/var/www/html/duelfi/profile_pics/'; // Directory to store the profile pictures
$fileExtension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
// Ensure the uploaded file is a valid image type (JPG, JPEG, PNG)
if (!in_array($fileExtension, ['jpg', 'jpeg', 'png'])) {
echo json_encode(['success' => false, 'message' => 'Invalid file type']);
exit;
}
// Define the target file path using privy_id
$uploadFile = $uploadDir . $privy_id . '.jpg'; // File will be saved as {privy_id}.jpg
// Ensure the directory exists and has proper permissions
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true); // Create directory if it doesn't exist
}
// Check if the file already exists and remove it before uploading the new file
if (file_exists($uploadFile)) {
unlink($uploadFile); // Delete the existing file
}
// Move the uploaded file to the target directory
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
// The image URL for the uploaded file (publicly accessible)
$imageUrl = 'https://vps.playpoolstudios.com/duelfi/profile_pics/' . $privy_id . '.jpg';
echo json_encode(['success' => true, 'imageUrl' => $imageUrl]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to move uploaded file']);
}
} else {
// Handle cases where no file is uploaded or privy_id is missing
echo json_encode(['success' => false, 'message' => 'No file uploaded or missing privy_id']);
}
?>