-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_contacts.php
More file actions
41 lines (31 loc) · 1.32 KB
/
Copy pathexport_contacts.php
File metadata and controls
41 lines (31 loc) · 1.32 KB
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
<?php
require_once 'auth.php';
requireLogin();
$user = getCurrentUser();
$pdo = connect_db();
// Récupérer tous les contacts de l'utilisateur
$stmt = $pdo->prepare("SELECT c.name, c.email, c.phone, cat.name as category_name, c.is_favorite, c.created_at FROM contacts c LEFT JOIN categories cat ON c.category_id = cat.id WHERE c.user_id = ? ORDER BY c.name");
$stmt->execute([$user['id']]);
$contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Nom du fichier CSV
$filename = 'contacts_' . date('Ymd_His') . '.csv';
// En-têtes pour le téléchargement du fichier CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// Ouvrir le flux de sortie
$output = fopen('php://output', 'w');
// Écrire l'en-tête du CSV
fputcsv($output, ['Nom', 'Email', 'Telephone', 'Categorie', 'Favori', 'Date de Creation']);
// Écrire les données des contacts
foreach ($contacts as $contact) {
fputcsv($output, [
$contact['name'],
$contact['email'],
$contact['phone'],
$contact['category_name'],
$contact['is_favorite'] ? 'Oui' : 'Non',
$contact['created_at'] ]); }
// Fermer le flux
fclose($output);
exit();
?>