|
@@ -0,0 +1,46 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+// upload.php
|
|
|
|
|
+
|
|
|
|
|
+// 1) En-têtes CORS
|
|
|
|
|
+header('Access-Control-Allow-Origin: http://127.0.0.1:5500');
|
|
|
|
|
+header('Access-Control-Allow-Methods: POST, OPTIONS');
|
|
|
|
|
+header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
|
+
|
|
|
|
|
+// 2) Répondre au preflight
|
|
|
|
|
+if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
|
|
|
+ http_response_code(204);
|
|
|
|
|
+ exit;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 3) Vérifier la présence du fichier
|
|
|
|
|
+if (!isset($_FILES['file'])) {
|
|
|
|
|
+ http_response_code(400);
|
|
|
|
|
+ echo json_encode(['success' => false, 'error' => 'Aucun fichier reçu']);
|
|
|
|
|
+ exit;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 4) Créer le répertoire d'uploads s’il n'existe pas
|
|
|
|
|
+$uploadDir = __DIR__ . '/uploads';
|
|
|
|
|
+if (!is_dir($uploadDir)) {
|
|
|
|
|
+ mkdir($uploadDir, 0755, true);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 5) Traiter l’upload
|
|
|
|
|
+$file = $_FILES['file'];
|
|
|
|
|
+$tmpName = $file['tmp_name'];
|
|
|
|
|
+$origName = basename($file['name']);
|
|
|
|
|
+$target = $uploadDir . '/' . $origName;
|
|
|
|
|
+
|
|
|
|
|
+if (move_uploaded_file($tmpName, $target)) {
|
|
|
|
|
+ // Succès
|
|
|
|
|
+ http_response_code(200);
|
|
|
|
|
+ echo json_encode([
|
|
|
|
|
+ 'success' => true,
|
|
|
|
|
+ 'filename' => $origName,
|
|
|
|
|
+ 'path' => 'uploads/' . $origName
|
|
|
|
|
+ ]);
|
|
|
|
|
+} else {
|
|
|
|
|
+ // Échec
|
|
|
|
|
+ http_response_code(500);
|
|
|
|
|
+ echo json_encode(['success' => false, 'error' => 'Impossible de déplacer le fichier']);
|
|
|
|
|
+}
|