| Current Path : /home/happyrenas/myreco.online/administration/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/happyrenas/myreco.online/administration/upload_photo_hebergement_multiple.php |
<?php
include("../configuration.php");
include("../includes/fonctions.php");
setlocale(LC_TIME, 'fr_FR.UTF-8');
header('Content-Type: application/json');
/**
* Redimensionne une image JPG vers une taille max
*/
function redimensionnerJPG($source_path, $destination_path, $max_width, $max_height)
{
$image = imagecreatefromjpeg($source_path);
if (!$image) {
return false;
}
$width = imagesx($image);
$height = imagesy($image);
$ratio = min($max_width / $width, $max_height / $height, 1);
$new_width = (int)($width * $ratio);
$new_height = (int)($height * $ratio);
$resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$result = imagejpeg($resized, $destination_path, 85);
imagedestroy($image);
imagedestroy($resized);
return $result;
}
$response = [
'success' => false,
'message' => '',
'photos' => []
];
// Paramètres
$root_upload_dir = DOSSIER_RACINE . '/upload/hebergement_multiple/';
$web_base_url = 'https://myreco.online/upload/hebergement_multiple/';
$max_file_size = 2 * 1024 * 1024; // 2 Mo
$max_width = 1600;
$max_height = 1600;
// Vérifications
if (empty($_POST['token'])) {
$response['message'] = 'Token manquant.';
echo json_encode($response);
exit;
}
if (!isset($_FILES['photos'])) {
$response['message'] = 'Aucun fichier reçu.';
echo json_encode($response);
exit;
}
$token = preg_replace('/[^a-f0-9]/', '', $_POST['token']);
$prefix = strtoupper(substr($token, 0, 2));
if (!$token) {
$response['message'] = 'Token invalide.';
echo json_encode($response);
exit;
}
// Vérifier que l'hébergement existe
$heb = $db->get_row("SELECT photos_local_json FROM heb WHERE token = '" . $db->escape($token) . "'");
if (!$heb) {
$response['message'] = 'Hébergement introuvable.';
echo json_encode($response);
exit;
}
// Dossier de destination
$dir = $root_upload_dir . $prefix . '/';
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
// JSON existant
$photos_json = json_decode($heb->photos_local_json ?? '[]', true);
$photos_json = is_array($photos_json) ? $photos_json : [];
$uploaded_count = 0;
$errors = [];
// Normalisation du tableau FILES pour upload multiple
$files = $_FILES['photos'];
for ($i = 0; $i < count($files['name']); $i++) {
if ($files['error'][$i] !== UPLOAD_ERR_OK) {
$errors[] = 'Erreur upload fichier : ' . $files['name'][$i];
continue;
}
if ($files['size'][$i] > $max_file_size) {
$errors[] = 'Fichier trop volumineux : ' . $files['name'][$i];
continue;
}
$tmp_name = $files['tmp_name'][$i];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $tmp_name);
finfo_close($finfo);
if ($mime !== 'image/jpeg') {
$errors[] = 'Seuls les fichiers JPG sont acceptés : ' . $files['name'][$i];
continue;
}
$filename = time() . '_' . bin2hex(random_bytes(5)) . '.jpg';
$absolute_path = $dir . $filename;
$relative_path = $prefix . '/' . $filename;
$photo_url = $web_base_url . $relative_path;
if (redimensionnerJPG($tmp_name, $absolute_path, $max_width, $max_height)) {
$photos_json[] = $relative_path;
$response['photos'][] = $photo_url;
$uploaded_count++;
} else {
$errors[] = 'Erreur traitement image : ' . $files['name'][$i];
}
}
// Sauvegarde JSON
$db->query("
UPDATE heb
SET photos_local_json = '" . $db->escape(json_encode($photos_json, JSON_UNESCAPED_UNICODE)) . "'
WHERE token = '" . $db->escape($token) . "'
");
$response['success'] = ($uploaded_count > 0);
$response['message'] = $uploaded_count . ' photo(s) enregistrée(s).';
if (!empty($errors)) {
$response['message'] .= ' Erreurs : ' . implode(' | ', $errors);
}
echo json_encode($response);
exit;
?>