Spaces:
Running
Running
Download php-api/file.php from aislynajay/product-development: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/aislynajay/product-development/resolve/main/php-api/file.php
- Command line
-
hf download hf://spaces/aislynajay/product-development/php-api/file.php
-
curl -L -o file.php https://huggingface.co/spaces/aislynajay/product-development/resolve/main/php-api/file.php
3.72 kB
| /** | |
| * 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, | |
| ]); | |