Spaces:
Running
Running
File size: 3,718 Bytes
7b90a6e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <?php
/**
* file.php — Image upload endpoint for the Farm AI backend.
*
* Deploy this at: https://aislyntech.com/Api/product_development/file.php
* (i.e. drop it in the same "product_development" folder as the existing
* upload.php on the aislyntech.com host.)
*
* Contract expected by app.py's upload_file_to_server() / direct calls:
* POST multipart/form-data
* image : file (required)
* type : string (optional, e.g. "original" / "predicted" — used as a subfolder)
*
* Response JSON:
* success: {"status": "success", "url": "https://.../uploads/<type>/<name>.jpg"}
* failure: {"status": "error", "message": "..."}
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
exit;
}
if (!isset($_FILES['image']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'No image uploaded or upload error']);
exit;
}
$file = $_FILES['image'];
// ---- Size limit (15MB) ----
$maxBytes = 15 * 1024 * 1024;
if ($file['size'] > $maxBytes) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'File too large (max 15MB)']);
exit;
}
// ---- Verify it's actually an image and detect real type (don't trust client mimetype) ----
$imageInfo = @getimagesize($file['tmp_name']);
if ($imageInfo === false) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Uploaded file is not a valid image']);
exit;
}
$allowedMimes = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/webp' => 'webp',
'image/gif' => 'gif',
];
$mime = $imageInfo['mime'];
if (!isset($allowedMimes[$mime])) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Unsupported image type: ' . $mime]);
exit;
}
$ext = $allowedMimes[$mime];
// ---- Subfolder from "type" (whitelisted characters only — no path traversal) ----
$type = isset($_POST['type']) ? preg_replace('/[^a-zA-Z0-9_-]/', '', $_POST['type']) : 'general';
if ($type === '') {
$type = 'general';
}
// ---- Ensure storage dir exists ----
$baseDir = __DIR__ . '/uploads/' . $type;
if (!is_dir($baseDir)) {
if (!mkdir($baseDir, 0755, true) && !is_dir($baseDir)) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Failed to create upload directory']);
exit;
}
}
// ---- Unique filename, save file ----
try {
$unique = bin2hex(random_bytes(8));
} catch (Exception $e) {
$unique = uniqid('', true);
}
$filename = date('Ymd_His') . '_' . $unique . '.' . $ext;
$destPath = $baseDir . '/' . $filename;
if (!move_uploaded_file($file['tmp_name'], $destPath)) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Failed to save uploaded file']);
exit;
}
@chmod($destPath, 0644);
// ---- Build public URL from the current request (works regardless of domain/subpath) ----
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$scriptDir = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');
$publicUrl = $scheme . '://' . $host . $scriptDir . '/uploads/' . $type . '/' . $filename;
echo json_encode([
'status' => 'success',
'url' => $publicUrl,
]);
|