/home/u764571690/domains/savitrfoundation.com/public_html
Edit: /home/u764571690/domains/savitrfoundation.com/public_html/reupload_document.php (6038B)
prepare("SELECT * FROM documents WHERE id = ? AND applicant_id = ?");
$stmt->execute([$documentId, $applicantId]);
$oldDocument = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$oldDocument) {
throw new Exception('Document not found');
}
// Verify the document type matches
if ($oldDocument['document_type'] !== $documentType) {
throw new Exception('Document type mismatch');
}
// Get applicant details to get the folder path
$stmt = $pdo->prepare("SELECT application_id FROM applicants WHERE applicant_id = ?");
$stmt->execute([$applicantId]);
$applicant = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$applicant) {
throw new Exception('Applicant not found');
}
// Use the same directory as the old document
$oldPath = $oldDocument['document_path'];
$directory = dirname($oldPath);
// Ensure directory exists
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
// Generate new filename with WebP extension
$fileName = $documentType . '_' . time() . '.webp';
$filePath = $directory . DIRECTORY_SEPARATOR . $fileName;
// Convert to WebP format
$webpPath = convertToWebP($file['tmp_name'], $filePath, 80);
if (!$webpPath) {
throw new Exception('Failed to convert image to WebP format');
}
// Get WebP file size for response
$webpSize = filesize($webpPath);
$originalSize = $file['size'];
// Begin transaction
$pdo->beginTransaction();
try {
// Delete old document file if it exists
if (file_exists($oldPath)) {
unlink($oldPath);
}
// Update document record in database
$stmt = $pdo->prepare("
UPDATE documents
SET document_name = ?, document_path = ?, status = 'pending', uploaded_at = NOW()
WHERE id = ? AND applicant_id = ?
");
$stmt->execute([
$file['name'],
$webpPath,
$documentId,
$applicantId
]);
// Commit transaction
$pdo->commit();
// Return success response with compression details
echo json_encode([
'success' => true,
'message' => 'Document re-uploaded and converted to WebP successfully. It will be reviewed shortly.',
'document_id' => $documentId,
'file_name' => $file['name'],
'original_size' => round($originalSize / 1024, 2),
'webp_size' => round($webpSize / 1024, 2),
'saved_percentage' => round((($originalSize - $webpSize) / $originalSize) * 100, 2)
]);
} catch (Exception $e) {
// Rollback transaction
$pdo->rollBack();
throw $e;
}
} catch (Exception $e) {
// Return error response
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>