Introduction

Pour réaliser vos exercices, créez un dossier exercices dans votre dossier de travail.

À l’intérieur de celui-ci, ajoutez le dossier langage, puis créez dans ce dernier un fichier index.php.

Si vous utilisez Herd, pensez également à placer votre dossier de travail dans le répertoire surveillé par Herd.

1
<?php
2
3
$basePath = realpath(__DIR__);
4
$currentPath = $basePath;
5
6
if (isset($_GET['path'])) {
7
    $requestedPath = realpath($basePath . '/' . $_GET['path']);
8
9
    // Sécurité : empêche de sortir du dossier racine
10
    if ($requestedPath && str_starts_with($requestedPath, $basePath)) {
11
        $currentPath = $requestedPath;
12
    }
13
}
14
15
$files = scandir($currentPath);
16
$files = array_filter($files, fn($f) => $f !== '.' && $f !== '..');
17
18
usort($files, function ($a, $b) use ($currentPath) {
19
    if (is_dir($currentPath.'/'.$a) && !is_dir($currentPath.'/'.$b)) return -1;
20
    if (!is_dir($currentPath.'/'.$a) && is_dir($currentPath.'/'.$b)) return 1;
21
    return strcasecmp($a, $b);
22
});
23
24
function icon(string $file, string $path): string {
25
    if (is_dir($path.'/'.$file)) return "📁";
26
    if (str_ends_with($file, '.php')) return "🐘";
27
    if (str_ends_with($file, '.html')) return "🌍";
28
    return "📄";
29
}
30
31
$relativePath = str_replace($basePath, '', $currentPath);
32
?>
33
<!DOCTYPE html>
34
<html>
35
<head>
36
<meta charset="UTF-8">
37
<title>Explorateur</title>
38
<style>
39
body { background:#0f172a; color:#e2e8f0; font-family:sans-serif; padding:40px; }
40
a { color:#38bdf8; text-decoration:none; }
41
a:hover { color:#facc15; }
42
.folder { font-weight:bold; }
43
.meta { color:#64748b; font-size:13px; margin-left:8px; }
44
</style>
45
</head>
46
<body>
47
48
<h1>📂 <?= $relativePath ?: '/' ?></h1>
49
50
<ul>
51
52
<?php if ($currentPath !== $basePath): ?>
53
    <li>
54
        ⬆️ <a href="?path=<?= urlencode(dirname($relativePath)) ?>">Dossier parent</a>
55
    </li>
56
<?php endif; ?>
57
58
<?php foreach ($files as $file): 
59
    $fullPath = $currentPath.'/'.$file;
60
?>
61
<li>
62
    <?= icon($file, $currentPath) ?>
63
    <?php if (is_dir($fullPath)): ?>
64
        <a class="folder" href="?path=<?= urlencode(trim($relativePath.'/'.$file,'/')) ?>">
65
            <?= htmlspecialchars($file) ?>
66
        </a>
67
    <?php else: ?>
68
        <a href="<?= htmlspecialchars($relativePath.'/'.$file) ?>" target="_blank">
69
            <?= htmlspecialchars($file) ?>
70
        </a>
71
        <span class="meta">(<?= round(filesize($fullPath)/1024,2) ?> Ko)</span>
72
    <?php endif; ?>
73
</li>
74
<?php endforeach; ?>
75
76
</ul>
77
78
</body>
79
</html>