-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.php
More file actions
36 lines (31 loc) · 1.06 KB
/
delete.php
File metadata and controls
36 lines (31 loc) · 1.06 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
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['songId'])) {
$songId = $_POST['songId'];
// Cargar el archivo JSON
$songs = json_decode(file_get_contents('songs.json'), true);
$updatedSongs = [];
// Buscar y eliminar la canción
foreach ($songs as $song) {
if ($song['id'] != $songId) {
$updatedSongs[] = $song;
} else {
// Eliminar el archivo de la canción y la portada
$songFilePath = 'uploads/' . $song['file'];
$coverFilePath = 'uploads/' . $song['cover'];
// Comprobar si los archivos existen antes de intentar borrarlos
if (file_exists($songFilePath)) {
unlink($songFilePath);
}
if (file_exists($coverFilePath)) {
unlink($coverFilePath);
}
}
}
// Guardar la nueva lista en el JSON
file_put_contents('songs.json', json_encode($updatedSongs, JSON_PRETTY_PRINT));
header('Location: music.php');
exit();
} else {
echo "Formulario no válido.";
}
?>