73 lines
3.3 KiB
PHP
73 lines
3.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// Define absolute paths for security and reliability
|
|
$uploadDir = '/home/ubuntu/executor/workspace/uploads/';
|
|
$pythonScriptPath = '/home/ubuntu/executor/workspace/ml/predict.py';
|
|
// It's better to use a specific python version if possible
|
|
$pythonExecutable = 'python3';
|
|
|
|
$response = [];
|
|
|
|
// Check if a file was uploaded and there were no errors
|
|
if (isset($_FILES['mri_image']) && $_FILES['mri_image']['error'] === UPLOAD_ERR_OK) {
|
|
$tmpName = $_FILES['mri_image']['tmp_name'];
|
|
|
|
// Sanitize the original filename and create a unique, safe path
|
|
$originalName = basename($_FILES['mri_image']['name']);
|
|
$safeName = preg_replace("/[^A-Za-z0-9._-]/", "", $originalName);
|
|
$fileExtension = pathinfo($safeName, PATHINFO_EXTENSION) ?: 'tmp';
|
|
$uniqueName = 'mri_' . microtime(true) . '.' . $fileExtension;
|
|
$uploadFilePath = $uploadDir . $uniqueName;
|
|
|
|
// Move the uploaded file from the temporary location to our uploads directory
|
|
if (move_uploaded_file($tmpName, $uploadFilePath)) {
|
|
// Prepare the shell command to execute the Python script
|
|
// escapeshellarg() is crucial for security to prevent command injection
|
|
$command = $pythonExecutable . ' ' . escapeshellarg($pythonScriptPath) . ' ' . escapeshellarg($uploadFilePath);
|
|
|
|
// Execute the command and capture the output
|
|
$ml_output = shell_exec($command);
|
|
|
|
// IMPORTANT: Clean up the uploaded file immediately after use
|
|
unlink($uploadFilePath);
|
|
|
|
if ($ml_output) {
|
|
// Decode the JSON output from the Python script
|
|
$ml_response = json_decode(trim($ml_output), true);
|
|
|
|
if ($ml_response && isset($ml_response['label']) && isset($ml_response['confidence'])) {
|
|
// If the response is valid, format it for the frontend
|
|
$response = [
|
|
'success' => true,
|
|
'prediction' => $ml_response['label'],
|
|
'confidence' => $ml_response['confidence'],
|
|
'filename' => htmlspecialchars($safeName)
|
|
];
|
|
} else {
|
|
$response = ['success' => false, 'error' => 'ML script returned invalid data.'];
|
|
}
|
|
} else {
|
|
$response = ['success' => false, 'error' => 'Failed to execute the ML model script. Check server logs for details.'];
|
|
}
|
|
} else {
|
|
$response = ['success' => false, 'error' => 'Failed to save the uploaded file. Check directory permissions.'];
|
|
}
|
|
} else {
|
|
// Handle various upload errors
|
|
$uploadErrors = [
|
|
UPLOAD_ERR_INI_SIZE => 'File is too large (server limit).',
|
|
UPLOAD_ERR_FORM_SIZE => 'File is too large (form limit).',
|
|
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
|
|
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
|
|
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
|
|
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
|
|
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
|
|
];
|
|
$errorCode = $_FILES['mri_image']['error'] ?? UPLOAD_ERR_NO_FILE;
|
|
$errorMsg = $uploadErrors[$errorCode] ?? 'An unknown upload error occurred.';
|
|
$response = ['success' => false, 'error' => $errorMsg];
|
|
}
|
|
|
|
echo json_encode($response);
|